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.
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.
This isn't very obvious:
but with an alternative declaration, like:
we can easily see that it's a 2 dimensional array of strings.
Edit: I used Data[2][3] to show that it's a 2D array. I used fixed size for the dimensions like 2 & 3 just for demonstration. Of course we can have:
or
char** Data[]
Ok, here is what I mean by 2-D array:
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
Data2
above 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
Data
andData2
) 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"
.It's slightly tricky to think of a practical use for an array of char**. Especially one with 70 elements.
However, suppose that I'm going to run 70 programs. As you probably know, program arguments are commonly passed as a
char** argv
parameter tomain()
(orchar*[] argv
, which in a function signature is the same thing). So if I wanted to store the argv pointers for all those programs, I'd used an array just likeData
. Obviously I'd also need some more memory elsewhere, for the actual strings and argv arrays to occupy, but it's a start.Initializing an array with
{NULL}
sets all its elements to NULL. This is a useful shorthand: you can initialize an array with{firstelement, secondelement, ...}
, but if you don't provide enough terms, all the rest are treated as 0.Just like any other array of pointers, initialized with {NULL}, what this looks like in memory is 70 NULL pointers sitting in a row. There's (usually) no difference in memory between a
char**
and any other object pointer. I think it is legal to write a weird implementation in which there is a difference, but don't hold your breath waiting to come across one.So, the difference between 70 NULL
char**
in a row and 70 NULLchar*
in a row is what would be on the other end of the pointer, if they weren't NULL. On the other end of achar**
is a pointer to a char. On the other end of achar*
is a char. Either the pointer, or the char, might be the first in an array, depending on how it's being used.This is, effectively, a pointer to a pointer to pointers. However, since a "pointer" is nothing but a location in memory, there really isn't much use in doing this over just doing char* Data[70], other than making it obvious that each char* is a pointer to another char*, instead of a pointer to char.
What you've got is an array of 70 pointers, each of which points to another pointer, each of those pointers point to a char. On an interesting note, arrays themselves are pointers so you have three levels of pointers.