We have the following code:
#include <stdio.h>
#define LEN 10
int main(void) {
int i;
int array[LEN];
int *p;
for (i = 0; i < LEN; i++) {
array[i] = i;
}
for (p = &array[0]; p < &array[LEN]; p++) {
printf("Address: %p ", p);
printf("Value: %d\n", *p);
}
return 0;
}
And it asks us to find out how many bytes the sever uses to store an integer variable, and print it on a newline, and it gives us the hint to use sizeof.
Now, I’m a bit of a noob with pointers, so this is probably a really quick question, but should I print:
printf("Size of int: %d", sizeof(p));
or sizeof(*p)
One prints 4, and the other prints 8. I was leaning toward 8 (which comes from simply p) as it refers to the memory location, not the variable it’s pointing to’s value, right? And 8 would mean 8 bits, so 1 byte would be the answer?
An easy solution to your particular problem is to use
sizeofon the type name:You’ll get the answers you need without any confusion.