I was doing some practice with strings of different types and returning their address so I could understand the concept of pointer arithmetic better.
I noticed that when using the printf function, and %p as the reference character, the address would increment by 4 + 1 bytes when using the & operand on the variable, and by 1 byte without it.
Here is an example of my code and it's output:
1 #include <stdio.h>
2 #include <string.h>
3
4
5 int main ()
6 {
7 char charString_1[] = "Hello";
8 printf("%s\t%s\t %p\t %p\n", charString_1 + 1, charString_1 + 1, &charString_1 + 1, charString_1 + 1);
The output was the following
ello ello 0x7ffe76aba5d0 0x7ffe76aba5cb
Looking at the last two hex numbers only, the address is 203 and 208 (in decimal) respectively. So the latter is a char + int value bigger than the former. if I increment by two (&charString_1 + 2) , the gap is now 2(char + int) = 10 bytes.
I understand this question might be ridiculous, but my search results have turned up nothing. I'm trying to understand how memory works, and become better at finding common faults in buggy code.