指针增量到指针数组

时间:2018-03-20 01:02:01

标签: c arrays pointers data-structures

指向指向数组的指针数组的指针可以通过运算符递增后递增(p ++),如果是,为什么?我知道数组和指针是第一个地址的同义词,所以它们不能被视为变量并赋值,但指向数组指针的指针可以是,为什么?

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>

int main()
{
/*let's start with:                                                         */
int a = 1, b = 2, c = 3;

/* and some pointers to int to populate an array with:                      */
int* pa = &a;
int* pb = &b;
int* pc = &c;

/* let's declare an array of 3 pointers_to_int                              */
int *array[3] = {pa, pb, pc};

int **ptr;      /* ptr is a pointer to a pointer to int                     */

/* using "array" in an assignment like below                                */
/* will make it decay to a pointer to a pointer (to int)                    */
ptr = array;    /* ptr should now point to the first element of array       */

if (*ptr == pa) printf("ptr points to the first pointer from array \n");

/* now incrementing ptr will make it point to the second pointer from array:*/
ptr++;
if (*ptr == pb) printf("ptr points to the second pointer from array! yay!\n");

/* but your first question was, can we do this if the elements (of array)   */
/* themselves point to arrays...                                            */

/* let's follow the same logic and make the arrays first                    */
int array_a[3] = {1, 2, 3};
int array_b[3] = {4, 5, 6};
int array_c[3] = {7, 8, 9};

/* now let's declare some pointers to such arrays:                          */
int (*pA)[3] = &array_a;
int (*pB)[3] = &array_b;
int (*pC)[3] = &array_c;  /* notice the difference:                         */
/* int *pA[3] would be an array of 3 pointers to int because the [] operator*/
/* has a higher precedence than *(pointer) operator. so the statement would */
/* read: array_of_3 elements of type_pointer_to_int                         */
/* BUT, "int (*pA)[3]" is read: pointer_A (points to) type_array_of_3_ints! */

/* so now we need a different array to hold these pointers:                 */
/* this is called an_ARRAY_of_3_pointers to_type_array_of_3_ints            */
int (*ARRAY[3])[3] = {pA, pB, pC};

/* along with a a double pointer to type_array_of_3_ints PTR:               */
int (**PTR)[3] = ARRAY;

/* and check the same thing:                                                */
if (*PTR == pA) printf("PTR points to the first pointer from ARRAY \n");

PTR++;

if (*PTR == pB) printf("PTR points to the second pointer from ARRAY! YAY!\n");

/* now for your second question: */


/* the reason that something can not just be assigned to an array,          */
/* like: array = ptr; is that the array is not the same as a pointer!       */
/* ptr contains one 8 byte value, and it can be reassigned to another value,*/
/* while array is a fixed basket that contains 5 such changeable values.    */
/* But, ptr = array; is correct because the meaning of "array", in the      */
/* context of this assignment, changes from "basket" to "a pointer to the   */
/* first value of the basket", so it's like assigning the first address from*/
/* the basket to an address container, which is fine...                     */

   return 0;
}

> $ clang prog.c -Wall -Wextra -std=gnu89 "-ansi"  output:  
> ptr points to the first pointer from array  
> ptr points to the second pointer from array! yay! 
> PTR points to the first pointer from ARRAY 
> PTR points to the second pointer from ARRAY! YAY!