Why can't I type the value with this pointer?

Following yesterday's question , I experimented with pointers a bit more. In particular, pointers of type int (*) [n]

Here is the code I wrote:

#include <stdio.h>

int main(void)
{
    int a[5] = {1, 2, 3, 4, 5};

    int (*p) [5] = &a;
    int *q = a;

    printf("\t\t\t\t\tp\t\tq\n\n");
    printf("Original Pointers: \t%20d%20d", p, q);
    printf("\n\n");
    printf("Incremented Pointers:\t%20d%20d", p+1, q+1);
    printf("\n\n");
    printf("Pointer values:         %20d%20d", *p, *q);
    printf("\n\n");

    return 0;
}

And here is the conclusion:

                                    p                 q

Original Pointers:             132021776           132021776

Incremented Pointers:          132021796           132021780

Pointer values:                132021776                   1
  • The pointer p, the increment jumps to 20. Is it because it is a type pointer int(*)[5]and therefore jumps to sizeof(int) * number of columnsin an array?

  • Both pand qhave the same value (but different types), and still using the indirection operator pI do not get the value in the first cell of the array, but instead I'll print out the value p. Why is this?

  • int *p = &a, (- , , p, , , &a p, &arr ( int ( * ) [5]) int *, p?

+6
3

p, , 20. , int (*) [5] , , sizeof (int) * ?

.

p q ( ), p , OF p print . ?

p , *p . . *p int * .

* . .

int * p = & a, (- , , p, . , , & a p, & arr ( int (*) [5]) int *, p?

, int *q = (int *)p, , , undefined, , - . , , . .


, :

printf("Original Pointers: \t%20p%20p", (void *)p, (void *)q);

int. void * , printf() . , void *, , , , .

+4

undefined , .

-, - :

UB. printf(), %p void *.

, C11, Β§7.21.6.1/P8

p

void. [...]

P9,

[....] - , undefined.

printf() - , , void *.


, .

Β§1. p, 20 . [...]

, . int (*) [5] / sizeof(int [5]), , . datd

Β§ 2. p q ( ), `p [....]

. p int (*) [5], *p int [5]. . , , . ( .....)

, , , , int *. , .

Β§ 3. int *p = &a, [....]

. . . , C. int * int (*) [5] ( ), . , .

+3

:

int *p = &a,

, . C. ( , ). . , .

. C, void*.


, , C , . C11 6.5.16.1:

6.5.16.1

:

- , , ;

.

- , , ;

.

- , ( , lvalue), , , , ;

() . . .

- , ( , lvalue), , - void, , , , ;

, void.

- , , - ;

.

- , _Bool, - .

bools.

+3

All Articles