Pointers pointing to an array of integers

Here I have a doubt with the exit.

Why is the conclusion the same?

   int (*r)[10];
   printf("r=%p  *r=%p\n",r,*r);
   return 0;

Platform - GCC UBUNTU 10.04

+5
source share
3 answers

Since the name of the array splits into a pointer to its first element.

   int (*r)[10]; 

It is a pointer to an array of integers 10.
rgives you a pointer.

This array pointer must be dereferenced to access the value of each element.
So, contrary to what you think **r, and not *r, you get access to the first element of the array.
*rgives you the address of the first element in an array of integers that matches ther

, :

, , , , , .

+7

, .

#include <stdio.h>

int main()
{
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int (*r)[10] = &a;

    printf("r=%p  *r=%p  *(r+0)=%p  *(r+1)=%p\n", r, *r, *(r+0), *(r+1));
    printf("sizeof(int)=%d \n", sizeof(int));

    return 0;
}

:

r=0xbfeaa4b4  *r=0xbfeaa4b4  *(r+0)=0xbfeaa4b4  *(r+1)=0xbfeaa4dc
sizeof(int)=4 

/ (-) --:

  • _DO_NOT _ , . , int (*r)[10]; , . .

  • , - * r , * (r + 0), r ( w.r.t )

  • * (r + 0) * (r + 1), 40 (0xbfeaa4dc - 0xbfeaa4b4 = sizeof (int) * ( 10). , , sizeof (type) !

, !

+4

, "N-element array of T" , " T", . , sizeof, & (), , .

:

int a[10] = {0};
printf("a = %p, &a = %p\n", (void *) a, (void *) &a);

printf a , "10- int" " int" , (&a[0]). &a " 10- int", , a ( ).

undefined , r , , , , . :

int a[10] = {0};
int (*r)[10] = &a;
printf("r = %p, *r = %p\n", (void *) r, (void *) *r);

r == &a *r == a.

The expression ris of type "pointer to a 10-element array int", and its value is an address a. The expression *ris of the type "10-element array int, which is converted to a" pointer to int", and its value is set to the address of the first element, which in this case is equal a[0]Again, the values ​​of the two expressions are the same.

+4
source

All Articles