char **Data[70]={NULL};
What is the correct terminology for this? How else could it be written? What does it look like in memory? I am reading many tutorials on pointers but I don’t see it in this syntax. Any help is appreciated. Thanks.
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.
This structure
is an array of 70 pointers to pointers to char. The compiler allocates
70 * sizeof(char**)bytes for this array, which assuming 32-bit pointers is 280 bytes.If you internally think of a “pointer to char” as a string, which isn’t true but it’s close enough, then this is an array of 70 pointers to strings. To make some ASCII art and pretend that you have allocated and filled some values….
You could do the above with code like this (error checking malloc return values skipped):
Think of it this way: Each entry in the array is a
char **. Each entry can point to an arbitrary location in memory, said location(s) beingchar *and thus being able to point to a null-terminated character array aka “string.”Note carefully the distinction between this and what you get when you allocate a 2D array:
The allocation of
Data2above gives you a 2-dimensional array ofchar *pointers, said 2-d array being allocated in a single chunk of memory (10 * 70 * sizeof(char*)bytes, or 2800 bytes with 32-bit pointers). You don’t have the ability to assign thechar **pointers to arbitrary locations in memory that you have with the single-dimensional array ofchar **pointers.Also note (given above declarations of
DataandData2) that the compiler will generate different code for the following array references:Here’s another way to think about this: Imagine that you have several arrays of pointers to strings:
You have an array of pointers to “array of pointer to char”. If you now print the value of
data[1][1], think of it like this:data[1]gets you a pointer to the arraytable1. Then the valuetable1[1]equals"Dog".