-1

i have a nested array and i am trying to pass it from one function to another

but on the other end there is no data

this is where it is initialized :

int myarray[4][23];
//myarray populated
int size = sizeof(myarray) / sizeof(myarray[0]);
std::cout << "AND " << size << std::endl;    //this gives me 4 as expected
function(myarray);

it then goes to another file and is used:

function(int myarray[][23]) {
    int size = sizeof(myarray) / sizeof(myarray[0]);
std::cout << "AND " << size << std::endl;    //this gives me 0
}

thanks in advance

user2771826
  • 1
  • 1
  • 5

1 Answers1

1

When you pass an array to function, it is decayed to the pointer and the ability to find out its size using the sizeof is lost. It returns the size of pointer instead. Have a look at: What is array decaying?

In case of function with the following prototype:

void function(int myarray[][23]);

the type of argument is actually int (*)[23] so sizeof(myarray) returns the size of pointer, which is 4 on 32bit systems and 8 on 64bit systems.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167