开发者

variable length array of fixed length arrays in C

开发者 https://www.devze.com 2023-04-12 12:58 出处:网络
I have a function that accepts a variable length array of 80 character strings. I defined the function like so:

I have a function that accepts a variable length array of 80 character strings. I defined the function like so:

void foo (开发者_StackOverflowchar namelist[][80] ...

in main() I do the following:

char **names = (char **)malloc(numNames * 80);
foo(names);

The type of names here is not correct. What is the appropriate way to type it as a variable length array of fixed length arrays?


names should be declared as:

char (*names)[80];

This declares names as a pointer to an array of 80 chars.

By the way, there's no need to cast the return value of malloc in C and many people consider it bad practice. See: 1, 2.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号