Assuming that you have code like below.
int x=10;
Here x is a variable that holds a value 10. The variable x is nothing but a label to some memory address.
x
+-------+
| 10 | 4050 which is x
+-------+
So value 10 is stored in 4050. Its always hard to remember memory address. So you make it easy by naming it and using that name.Here x is nothing but a label to memory address 4050.
int *x_ptr;
Here x_ptr is a pointer variable that can store memory address of any integer variable.
+-------+
| GBV | 5000 which is x_ptr
+-------+
Here x_ptr is a label for address 5000. Initially the variable will contain Garbage Value(GBV).
x_ptr = &x;
The above statement stores address of x in x_ptr.
+-------+
| 4050 | 5000
+-------+
x_ptr = &x = 4050
printf("%u",x_ptr); // 4050
printf("%d", *x_ptr); // 10
*x_ptr = *(4050) = valueAt(4050) = 10
Let us now discuss about your code.
int *ptr;
+-------+
| GBV | 6000 which is ptr
+-------+
int array[4] = {1,2,3,4};
+-----------+
| 1 | 7824 which is array+0 or array[0]
+-----------+
| 2 | 7828 which is array+1 or array[1]
+-----------+
| 3 | 7832 which is array+2 or array[2]
+-----------+
| 4 | 7836 which is array+3 or array[3]
+-----------+
Here array = 7824
int *arrayEnd = array + n;
Incrementation with memory location will increase no of bytes based on the data type.
array+0 = 7824 + 0 = 7824
array+1 = 7824 + 1(4 bytes in case of integer) = 7824 + 4 = 7828
when n=4
arrayEnd = 7824 + 16 = 7840
+-------+
| 7840 | 6010 which is arrayEnd
+-------+
for (ptr = array; ptr < arrayEnd; ++ptr)
sum += *ptr;
Here ptr is initialized with array's base address(7824). So 7824 will be stored in memory 6000.
6000 6000
+-------+ +-------+
| GBV | => | 7824 | 6000 which is ptr
+-------+ +-------+
so now ptr = 7824
For Loop
+-------+--------------+-------------+
| ptr | *ptr | sum + =*ptr | // 7824 < 7840
+-------+--------------+-------------+
| 7824 | *(7824) = 1 | sum += 1 | // 7828 < 7840
+-------+--------------+-------------+
| 7828 | *(7828) = 2 | sum += 2 | // 7832 < 7840
+-------+--------------+-------------+
| 7832 | *(7832) = 3 | sum += 3 | // 7836 < 7840
+-------+--------------+-------------+
| 7836 | *(7836) = 4 | sum += 4 | // 7840 !< 7840 so loop terminates
+-------+--------------+-------------+
Why ptr = array instead of *ptr = array?
valueAt(*) has to be used when you have to store some value in the memory address.
ptr= array; //initializing base address of array to ptr.
6000 6000
+-------+ +-------+
| GBV | => | 7824 | 6000 which is ptr
+-------+ +-------+
*ptr = array; // ptr already has a address pointed to, and you store array's base address inside that memory location.
6000
+-------+
| GBV | *ptr= *(GBV) = Unknown = array // No guarantee that GBV is a proper memory address
+-------+
if ptr already has some proper memory address say 1000, then it is like
6000
+-------+
| 1000 | *ptr= *(1000) = valueAt(1000) = array
+-------+
which makes
1000
+-------+
| 7824 |
+-------+
Simple example with multi pointers.
#include<stdio.h>
int main()
{
int x=10;
int *y=&x;
int **z=&y;
printf("%d --> %d --> %d",x,*y,**z);
return 0;
}
OUTPUT:
10 --> 10 --> 10
array. Also, I thinksum += *ptr;should be either indented or inside{}.func(ptr)andfunc(*ptr)can both be legitimate, but they do different things.func(ptr)passes the address thatptrcontains, whilefunc(*ptr)passes the value found at the address whichptrcontains.nandsumcome from? I assume they're both justints?function(ptr);doesn't make any sense. There's no return type forfunctionand no type at all forptr.