What are the differences between an array of char pointers and a 2D array?
-
A really good resource on understanding this is this http://www.gamedev.net/page/resources/_/technical/general-programming/a-tutorial-on-pointers-and-arrays-in-c-r1697 – John Riselvato Oct 21 '11 at 04:03
-
Read: [Difference between `char* str[]` and `char str[][]` and how both stores in memory?](http://stackoverflow.com/questions/17564608/what-does-the-variable-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) – Grijesh Chauhan Jul 15 '13 at 18:40
4 Answers
char* pasz[3] = {"abc", "def", "ghi"};
char asz[3][] = {"abc", "def", "ghi"};
The similarities and differences are basically the same as between these two:
char *psz = "jkl";
char sz[] = "jkl";
The first is originally read-only.
psz[0] = 'a'; // Illegal!!
The second, you can modify, since you allocate it with the []
.
sz[0] = 'b';
// sz == "bkl"
The first, you can modify what it points to:
char mysz[] = "abc";
psz = mysz;
psz[0] = 'b';
// mysz == "bbc"
The second, you cannot:
sz = mysz; // Can't assign an array to an array!!

- 24,552
- 19
- 101
- 135
-
-
@Failed_Noob By removing one dimension of array `[]`. (`char*[]` to `char*`. And `char[][]` to `char[]`.) It is a lot easier to think of it that way - they're basically the same thing, except in the first code snippet, we have another dimension of complexity. For teaching purposes, I removed this complexity. – Mateen Ulhaq Oct 21 '11 at 04:10
-
`char asz[][] = {"abc", "def", "ghi"};` is not valid declaration through, Column index is mandatory here. – Grijesh Chauhan Jul 10 '13 at 13:45
char* my_string[];
represents an array of strings.
int my_grid_of_ints[][];
char my_block_of_text[][];
If color = byte[3]
then you could represent your screen monitor
color my_pixel_buffer[][] = new color[768][1024];
is a 2D array. As you can see, a 2D array can represent anything, including an array of char pointers (such as multiple lines of strings).

- 6,163
- 6
- 56
- 83
-
-
-
Is my syntax wrong? Sorry, I havent used C ever, only C++. Also, it has been some time - I'm used to C#! – Caleb Jares Oct 21 '11 at 04:23
-
A `int my_ints[][];` would be 2D. It is the same in C++. (I am a C++ programmer, so my C skills aren't too great either. :)) – Mateen Ulhaq Oct 21 '11 at 04:31
-
Oh gosh. My brain interpreted 2D as left-right [0-size] instead of [0-size][0-size]. It must be that time of night! – Caleb Jares Oct 21 '11 at 04:37
-
Nope - still thursday here, but barely! On another note, it is 10/20 today, and that is the date of my bios, so it is messing with me - making me think that I overclocked wrong and reset the bios :P – Caleb Jares Oct 21 '11 at 04:45
-
1
You can access elements with the same syntax, but the guarantees about memory layout is much different. The 2d array is contiguous. The array of pointers is not.

- 53,214
- 7
- 75
- 105
-
This also means that you can use memcpy() and similar functions of the former, but not on the latter. Also, you can't cast between 2d arrays (array of arrays) and pointer-to-pointers. – Lundin Oct 21 '11 at 06:25
Array of arrays (aka multi-dimensional array) looks like (in memory):
a[0][0], a[0][1], a[0][n-1], a[1][0], a[1][1], ..., a[1][n-1], ..., a[m-1][n-1]
array of pointers looks like:
p[0], p[1], ..., p[m-1]
where each slot is a pointer and can point to whatever. If they all happen to point to arrays with n
elements each, then p[i][j]
and a[i][j]
can be used similarly in expressions, but they're actually quite different objects.

- 208,859
- 35
- 376
- 711