指向指针数组的指针能否用p++后置自增?数组与指针区别解析
p++? And why can such pointers be assigned while arrays cannot? Great question—this gets to some core distinctions between pointers and arrays in C that trip up a lot of folks. Let's break this into two parts to make it clear.
Part 1: Yes, you can use p++ on a pointer to a pointer array (here's why)
First, let's clarify what we're talking about with an example:
- A pointer array is something like
int *arr[5];—this is an array of 5 pointers toint. - A pointer to this pointer array would be declared as
int *(*p)[5];—this pointerppoints to an entire array of 5int*elements.
The postfix increment operator p++ works here because:
pis a modifiable pointer variable. Unlike array names (which are not variables),pstores the memory address of the pointer array, and we can change that stored value.- When you increment a pointer in C, it doesn't just add 1 to the address—it adds the size of the type the pointer points to. In this case,
ppoints to an array of 5int*s, sop++will shift the address bysizeof(int*) * 5bytes, pointing it to the next contiguous array of the same type (if one exists).
Here's a concrete code example to demonstrate:
#include <stdio.h> int main() { int a = 1, b = 2, c = 3, d = 4; int *arr1[2] = {&a, &b}; int *arr2[2] = {&c, &d}; // p points to the pointer array arr1 int *(*p)[2] = &arr1; printf("Address of arr1: %p\n", (void*)&arr1); printf("p before increment: %p\n", (void*)p); p++; // This is valid! printf("p after increment: %p\n", (void*)p); // Now p points to arr2 (since arr2 is stored right after arr1 in memory) return 0; }
The only time this wouldn't work is if p were declared as a const pointer (e.g., int *(*const p)[5];), which prevents modifying the pointer's value.
Part 2: Why arrays can't be assigned like variables, but pointer-to-pointer-arrays can
This boils down to a key difference between array names and pointer variables:
Arrays are not modifiable lvalues
An array name (like arr1 in the example above) is an identifier that represents a contiguous block of memory. While it often implicitly converts to a pointer to its first element in expressions (e.g., when you pass it to a function), the array name itself is a non-modifiable lvalue:
- You can't do
arr1 = arr2;because the array name doesn't represent a variable that holds an address—it represents the array itself. C doesn't allow assigning entire arrays directly with the=operator (you'd usememcpyor a loop to copy elements instead). - You also can't do
arr1++because the array name isn't a variable you can modify—it's a fixed reference to the start of the array's memory.
Pointer variables are modifiable lvalues
A pointer like p (the pointer to our pointer array) is a regular variable. Its sole job is to store a memory address. Since it's a modifiable variable:
- You can assign it new values (e.g.,
p = &arr2;), which just changes the address it holds. - You can increment/decrement it (like
p++), which adjusts the stored address based on the pointer's target type.
In short: Arrays represent blocks of memory; pointers are variables that hold addresses to memory blocks. That's why pointers can be modified and arrays can't.
内容的提问来源于stack exchange,提问作者Navneet Kumar

