4

I have this struct:

struct noduri {
    int nod[100];
};

and this function:

int clearMatrix(int var)
{
    cout << all[1].nod[30];
}

int main()
{
    noduri all[100];
    cout << all[1].nod[30];
    return 0;
}

and I want the struct to be assigned to all 100 elements of array all[], when I do cout << all[1].nod[30]; everything works fine, no errors, it outputs 0. When I call clearMatrix(1) I get this error : error: request for member nod in all[1], which is of non-class type int, what am I doing wrong ?!

4 Answers 4

6

The array variable all is local to the main function, so you cannot reference it in clearMatrix unless you pass a pointer to it into the function:

int clearMatrix(int var, noduri *all)
{
    cout<<all[1].nod[30];
}


int main()
{
    noduri all[100];
    clearMatrix(5, all);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

3

you are reffering in the function that array which is not in its scope, you need to do it as

int clearMatrix(int var,noduri *all)
 {
   cout<<all[1].nod[30]; // as here you have the base address of the array of type noduri you can refer it.
 }
 int main()
 {
noduri all[100];
clearMatrix(5, all);
return 0;
}

Comments

1

You are using raw arrays. That's not a good idea. Use std::vector if the size if not known at compile time, consider std::array if it is known at compile time and dynamic resizing would cause measurable performance problems.

One of the problems with raw arrays in C++ is that it's not at all(!) as easy to pass them to a function like, say, an int or a double. std::vector and std::array, in contrast, are as easy to pass to a function like any other normal type.

Here's a complete example:

#include <array>
#include <iostream>

struct noduri {
    std::array<int, 100> nod;
};

void clearMatrix(std::array<noduri, 100> const &array) {
    std::cout << array[1].nod[30];
}

int main() {
    std::array<noduri, 100> all;
    std::cout << all[1].nod[30];
}

Note that std::array is only available if your compiler supports C++11. For an older compiler, use boost::array or just do it with a std::vector.

Comments

1

The code you showed will not be compiled and has no any sense. If I have understood correctly you want to assign each element of the array by some value in function clearMatrix. If so then the code will look the following way

#include <iostream>

struct noduri
{
    int nod[100];
};

int clearMatrix( noduri *matrix, int size, int var )
{
   for ( int i = 0; i < size; i++ )
   {
      for ( int &n : matrix[i].nod ) n = var;
   }
}

int main()
{
    const int N = 100;   
    noduri all[N] = {};

    std::cout << all[1].nod[30] << std::endl;

    clearMatrix( all, N, 10 );

    std::cout << all[1].nod[30] << std::endl;

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.