1

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

void foo (char 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?

1
  • You should not cast the return value of malloc in C. Commented Oct 11, 2011 at 19:01

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. Though I think the advantages of casting malloc outweigh the disadvantages. To cast here: (char (*)[])

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.