pass 2-dim array as param

S

Serve La

Why doesn't this work?

void myfunc(char **arr)
{
for (int i = 0; i < 3; i++) // access violation
puts(arr);
}

int main(void)
{
char arr[3][2] = { "1", "2", "3" };

for (int i = 0; i < 3; i++)
puts(arr); // prints correctly

myfunc(arr);
return 0;
}
 
A

Andreas Kahari

Why doesn't this work?

void myfunc(char **arr)

void myfunc(char arr[][2])

[cut]
int main(void)
{
char arr[3][2] = { "1", "2", "3" };
[cut]


If arr in main is an array of string literals of a certain
length, then you need to recieve it as such in your function
as well, otherwise your function has no way of knowing how far
apart e.g. arr and arr[i + 1] are.

Not that [3] in 'char arr[3][2] = { "1", "2", "3" };' may be
shortened into [].
 
S

Serve La

Andreas Kahari said:
If arr in main is an array of string literals of a certain
length, then you need to recieve it as such in your function
as well, otherwise your function has no way of knowing how far
apart e.g. arr and arr[i + 1] are.


ah yes, I see now. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top