C语言中char*类型元素地址疑问:指针数组元素内存不连续时如何寻址?
Great question—this gets right to how C manages memory for different array types, so let's unpack it clearly.
First: The 2D Character Array (char strings[3][10])
When you declare:
char strings[3][10] = {"Apple","Banana","Grape"};
You're creating a 2D array of chars, where each element is a fixed-size 10-byte char array. Here's what's happening:
- The compiler allocates 3 contiguous blocks of 10 bytes each (total 30 bytes) on the stack (if this is a local variable).
- Even though "Apple" only needs 6 bytes (5 letters + null terminator), the array reserves 10 bytes per element. The extra 4 bytes are either filled with nulls or undefined values, but the space is fixed.
- That's why the address gap between
strings[0]andstrings[1]is exactly 10 bytes—each element takes up a full 10-byte slot.
Now: The Pointer Array (char *pstrings[])
Your observation about "non-contiguous memory" is probably referring to the strings the pointers point to, not the pointer array elements themselves. Let's clarify:
When you declare:
char *pstrings[] = {"Apple","Banana","Grape"};
You're creating an array of char pointers, not an array of char arrays. Here's the key breakdown:
- The pointer array elements are contiguous:
- Each element in
pstringsis achar*(pointer), which has a fixed size based on your system: 4 bytes on 32-bit systems, 8 bytes on 64-bit systems. - The compiler allocates contiguous space for these pointers. So
&pstrings[0]and&pstrings[1]will be separated by exactlysizeof(char*)bytes (8 on 64-bit), and&pstrings[1]to&pstrings[2]is the same. The pointers themselves are stored consecutively.
- Each element in
- The strings they point to might not be contiguous:
- The string literals "Apple", "Banana", "Grape" are stored in the program's read-only data segment (not the stack). The compiler can place these literals anywhere in that segment—they don't have to be next to each other. But that's unrelated to how the pointer array accesses its own elements.
How the Program Finds the Next Element in a Pointer Array
C arrays work using base address + offset calculation, and the compiler handles this automatically at compile time:
- Every array has a base address (the address of the first element,
pstrings[0]). - To get the address of
pstrings[i], the program calculates:base_address + i * sizeof(element_type). - For the pointer array,
element_typeischar*, so the offset for each index isi * sizeof(char*).
For example, on a 64-bit system:
pstrings[0]is at address0x1000(hypothetical)pstrings[1]is at0x1000 + 1*8 = 0x1008pstrings[2]is at0x1008 + 1*8 = 0x1010
The compiler knows the size of char* when it compiles your code, so it generates the correct offset math to jump from one pointer element to the next.
Quick Recap
- 2D char array: Elements are fixed-size char arrays, gaps equal to the size of each subarray (10 bytes here).
- Pointer array: Elements are contiguous pointers, gaps equal to the size of a pointer. The strings they point to may be non-contiguous, but that doesn't affect how the array accesses its own elements.
内容的提问来源于stack exchange,提问作者moveax420

