The PaperBirdMaster response is excellent for intuitive understanding of the idea.To give some more "technical" or low-level detail, I will add the following:A pointer is a variable that contains a number. That number is a memory address. There's no mystery at the moment. The number you can see if you print it. For example:int vector[4];
int *p;
p = vector;
printf("%p\n", p);
There are a few details to be noted in this code.The pointer pat the time it is declared, it still contains no value. It's a pointer without initializing. Just like you declare a variable int n And you don't give him any value, he's not initializing.An uninitialized variable may be taken by default the value 0, but may not. It depends on the compiler. You can generally think it contains a data random. In the case of p, since it represents a memory address, we would say aiming to a random address. That's dangerous because through p you can modify what is in that direction and the consequences will be unpredictable (most likely the program will die with a segmentation faultBut even worse things can happen.It is therefore important to initialize the pointers as soon as possible. In this code I initialize by pointing to another variable. Another typical way to initialize it is to use malloc() to get new memory for our process and make the pointer point to that new memory.To make it point to another pre-existing variable the usual syntax would be p = &otra_variable, being the operator & which allows to obtain in which direction is that other variable. However if the other variable is an array, the &Since by definition, the name of an array represents the memory address in which is its first element. I mean, a It's the same thing. &a[0]. That explains why I can do p=a.In the printf() I used the format %p, which is typical when you want to see the value of a pointer, because it shows it in hexadecimal, which is usually more useful format for the programmer. But in the background it's just a number, you see it in hexadecimal or not (consisting) %d You could see it in decimal too). On the other hand, observe that I print the value of p. I haven't decorated. p with nothing in front. If I had written &p what I would see is in which direction is stored p. Putting p I see what value it stores p (i.e. the address to aim). Putting p would see the value pointed (see dereference later).When running that program on screen appears 0x7ffd32b6f8f0, although the specific number can vary in each execution. That's the number inside the pointer, and it's nothing but the memory address where the first element of the array is. a.Arithmetic pointersThe arithmetic consists of add or subtract an integer to the pointer. For example p+1 sums up 1.You might think the result would be 0x7ffd32b6f8f1 (1 more than the original value I had p). But the arithmetic of pointers is weird.and it has to be for the reason PaperBirdMaster explained in his response.When you add 1 to a pointer you don't want to move forward next memory addressbut the Next (as in the analogy with the directions of the houses, you don't want to advance one meter through the sidewalk, but the amount of meters necessary to reach the next house).In the case of pointers, that means when you write p+n, being n an integer, the compiler actually does p+ nsizeof(int)in this case, since p declared as a pointer to int. I mean, he's joined to p the amount of bytes needed to reach the next integer.If the size of a int It's 4, then. p+1 it will actually add 4 to p. The result will therefore be 0x7ffd32b6f8f4.You can check with the following code:printf("%p\n", p);
printf("%p\n", p+1);
Though mentally you can simply read p+1 as "the address of the next int after the address signed by p"and forget that the compiler actually adds 4. In general you will add the number of bytes that occupies the type pointed by p. This is one of the reasons you have to declare the guy pointed out by p.DisreferenceWhen you put on a * in front of an expression that is "point type" (i.e., it's a memory address), then you'll be accessing to content from that direction.This way if you put pYou'll be accessing what's in the memory address. 0x7ffd32b6f8f0 which, as we know, is the first element of the array a. You can change it if you do p = 3For example. Or read it if you do for example printf("%d\n", p).And putting this together as explained above, (p+1) will help you access the Next element within the array a.Note that if you try to access (p+8)for example, it would be equivalent to trying to access a[8]. Since that element does not exist, there will be problems.All right.Let us use these knowledge to solve the problem you pose. An array of 5 elements and the use of a pointer to travel and show the results.You have two ways to do it:Use type expressions (p+i) to access each element of the array (i would be an integer that would represent the index within the array.Use type expressions p = p +1 (or equivalently) p++) for increase the pointer himself pso you access each element of the array.Solution with the first approach:#include <stdio.h>
int main(void) {
int a[5] = {1, 4, 7, 9, 3};
int *p = a;
for (int i=0; i<5; i++){
printf("a[%d] = %d\n", i, *(p+i));
}
}
Using the second approach (I'll do it in this case with a loop while instead of a forFor a change, but you could do it too. for(c):#include <stdio.h>
int main(void) {
int a[5] = {1, 4, 7, 9, 3};
int *p = a;
int i = 0;
while (i<5) {
printf("a[%d] = %d\n", i, p);
i++;
p++;
}
}
The most important difference between the two approaches is that in the first case, at the end of the loop, p still points to the first element of the array, since We don't change its value. in the loop. In the second case instead when you leave the loop p it will be pointing out already outside the array, since in each iteration we are "increasing" its value (I suppose to increase in quotation marks because, due to the arithmetic of pointers, it is not a normal increase of 1 in 1, but of sizeof(int) in sizeof(int).Depending on the context you may be more interested in one approach or another.Bonus. Syntax (p+i) It is so common that the C designers invented a simpler alternative syntax: p[i]. This syntax however is very confusing for beginners, as it is exactly the same syntax used with the arrays. And it is intentional that it is the same syntax, because it is the context in which it is used, a pointer that points to an array.In fact, generally, when the compiler sees something with the paint: x[y], what actually runs is (x+y). That's why it works both with pointers and in p[2]that will lead (p+2)like in "true" arrays, like a[2], since the expression will be interpreted as *(a+2)And we've seen that a (the name of the array) is equivalent to the memory address where its first element is, so a+2 will also be making arithmetic pointers to access two integers beyond.