I was comparing various printf to better understand the difference between int * and int (*)[] and how i can visualize various addresses and values.
In the following program i wrote one thing bothers me:
#include <stdio.h>
int main() {
int a[3] = {1,2,3};
int *p = a;
printf("address of a: %p\n", &a);
printf("\naddress of a[0]: %p\n", &a[0]);
printf("\nvalue of p: %p\n", p);
printf("\nvalue of *p: %d\n", *p);
printf("\nvalue of a[1]: %d\n", *(p + 1));
puts("\n\n-------------------\n\n");
int b[3] = {1,2,3};
int (*q)[3] = &b;
printf("address of b: %p\n", &b);
printf("\naddress of b[0]: %p\n", &b[0]);
printf("\nvalue of q: %p\n", q);
printf("\nvalue of *q: %p\n", *q);
}
In the first part p, as a pointer, holds as value the address of a[0], so *p holds as value 1 ( printed with %d).
In the second part, though, q seems to hold the same value of *q (in this case i use %p), therefore i need to use **q to print b[0].
How come?
*pPOINTS at the value (that happens to be 1)...pis a variable whose datatype ispointer to intmeaningpcan hold a memory address (point to), in this case, anint... I hope this helps...q + 1(not*(q + 1), beware), compared top + 1. Please note that you should cast tovoid *, when using%p.int *p = a;is exactly the same as (shorthand for)int *p = &a[ 0 ];Perhaps seeing this will make it more obvious thatpis assigned the address of the first element ofa[ ]...