I know how to allocate memory to 2-D array but if the ptr is defined as int *ptr[][100];
Then how can I allocate memory for ptr ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Quoting from section 6.2.5 of the C standard:
thus, to use
int *ptr[][100];you have to link against a definition of it. If you specify that length then you have a 2D array of pointers toint. At this point you have to allocate a chunk of memory large enough for the 3D array, and assign correctly eachint *in the 2D array.A more straightforward approach could be the following:
The last line allocate a pointer to an array of an array of int, that can be effectively used like a 3D array. For instance:
The accepted answer to this question gives a really nice and precise overview of the use of pointers to array of types.