How do I know address of each position of the vector in C?

2

I'm having trouble knowing the vector address and address of each vector position.

#include <stdio.h>

int main(){

int vec[]={52,13,12,14};

printf("Endereço de vetor %d",&vec);
printf("vec[0]%d,vec[1]%d,vec[2]%d,vec[3]%d\n", &vec[0],&vec[1],&vec[2],&vec[3]);

return 0;
}
    
asked by anonymous 26.09.2016 / 23:16

1 answer

4

To get the vector address simply use its own variable, as I had already informed in comment on another question . The point is that the placeholder of correct printf() formatting is %p to receive a pointer. This will require a cast (void *) (generic pointer) to fit correctly (at least in compilers with more secure encoding options).

The same goes for the element values. But there you will use the & operator since it is normal to get their values.

#include <stdio.h>

int main(){
    int vec[] = {52, 13, 12, 14};
    printf("Endereço de vetor %p\n", (void*)vec);
    printf("vec[0] = %p, vec[1] = %p, vec[2] = %p, vec[3] = %p\n", (void*)&vec[0], (void*)&vec[1], (void*)&vec[2], (void*)&vec[3]);
}

See working on ideone .

    
26.09.2016 / 23:37