Confused in C
I just found pointers in my tutorial, but it doesn't explain enough, so I need help.
I know what pointers are and what they do. for example, I understand the following example very well:
#include <stdio.h>
int main ()
{
int num = 5;
int *point;
point = #
*point = 8;
printf("%d %d", num, *point);
return 0;
}
Point points to num (storing the number num as a value). and then I search for a point to change the original num value.
now consider a visually modified version of the same example:
#include <stdio.h>
int main ()
{
int num = 5;
int *point;
point = 8;
printf("point: %d\n", point);
printf("sum (%d) = num (%d)+ point (%d)", num+point, num, point);
return 0;
}
I have several questions:
1- why is it even possible to assign a normal value (8) to a pointer (point)? shouldn't pointers only store the addresses of other things? what happens on line 8?
2- I compiled the code and for the second printf it shows: sum (28) = num (5) + point (8) why is the sum equal to 28? 5 + 8 - 13. What's going on?
source to share
You can point point
to memory address 8 (or 0x00000008). This is equivalent to point = (int*)8;
I am getting a friendly error <Unable to read memory>
because I am using C ++ and I believe it protects me from my stupidity.
Why 5 + 8 = 28? If you add n to a pointer of type T, you move that pointer by n elements of type T. Your machine is dealing with 32-bit ints, which are 4 bytes, so 8 + (5 * 4) = 28.
source to share
In your second example, you are assigning 8 to a pointer. Hope you got a good warning from your compiler. But you can assign 8. Just don't track it down. You will likely get a segmentation fault or worse. The num + point sum makes sense. It was called pointer arithmetic. point is a pointer to an integer. Int on your computer is 4 bytes. The sum of num + point is (5 * sizeof (int)) + 8, which is 28 on your system.
source to share
Because virtually all C compilers implement pointers as integers to support pointer arithmetic.
int arr[10];
int *ip = arr;
arr[2] = 42; // this is equivalent to:
*(ip + 2) = 42; // or
*(2 + ip) = 42;
Let's say &arr[0]
(address of the first element of an integer array arr
) is 0xcafe0000
. Let's say one value int
takes up 4 bytes in memory. Then &arr[1]
is in 0xcafe0004
, &arr[2]
is in 0xcafe0008
, etc. In 4-byte increments.
The compiler sees this:
int i = arr[5]; // which is equivalent to:
int j = *(arr + 5);
and take the address 0xcafee0000
and add (5 * sizeof(int))
that goes to 0xcafee0028
before dereferencing it.
This is why when you type the integer ol 'into a pointer type say:
int *p = (int*) 8;
And then add it:
p = p + 5; // p := 28
The compiler will take the value p
and add it to the operand 5
times the sizeint
.
source to share
A pointer is a number, just as it int
is a number. Hard coded assignment to pointer is legal, but warning is issued
point = 8;
but pointless if you are not working with an embedded system and do not know what is at this address 8.
Even though your program retains memory access restrictions, you do not know what the value was in memory address 8. Thus, you will not understand the result of the calculation. This is Undefined Behavior because the program did not set a value at this address.
source to share
1 - why is it even possible to assign a normal value (8) to a pointer (point)? shouldn't pointers only store the addresses of other things? what happens on line 8?
You are correct that a pointer stores a memory address, but that memory address is simply written as a number. Typically, the values ββof pointers are much larger, but nothing prevents you from assigning a pointer to a memory address (8). Don't expect to use this address. One of the ways that is commonly used is NULL pointers .
int *point = NULL;
coincides with
int *point = 0;
2- I compiled the code and for the second printf it shows: sum (28) = num (5) + point (8) why is the sum equal to 28? 5 + 8 - 13. What's going on?
Pointer arithmetic doesn't work the way you might expect, and also for storing a memory location, a pointer has a memory type too:
int *point;
in this case, point
knows what he is pointing to int
. Therefore, when a pointer does not increment its position by one byte, it is incremented by sizeof(int)
, which in your case is 4 bytes. This means that point++
or point = point + 1
will move the pointer along whatever size it thinks it is currently pointing at, is the reason it makes it easier to traverse an array or block of memory of objects of the same type.
To get around this concept, try messing up this piece of code
#include <stdio.h>
int main()
{
int x;
int * ptr = &x;
for(int i = 0; i < 5; i++)
{
printf("pointer address: %p\n", ptr + i);
}
return 0;
}
In particular, try changing the type from x
and to or and see the difference it makes!ptr
int
char
long long
source to share
The point is that computer pointers are simply an integer value that has special meanings, that is, the values ββof a memory address. Hence, assigning integer values ββto a pointer is always possible, although you will probably end up with a null pointer.
The reason the num + pointer is 28 is because your computer is the reason why your addresses are 32 bits in the computer you are using. So C has this feature called pointer arithmetic, which means adding / subtracting any value to the pointer will actually add 4 * num, since there are 4 bytes in every 32 bit word your machine has. As a result, you get the dot + num == 8 + (4 * 5).
=== EDIT
Just as optional, when you do pointer arithmetic in C, the machine will actually add to the pointer the number of bytes that belong to your data type. For example, if you state:
int num = 5;
char * point;
then when you do
point = 8;
the result of dot + num == 8 + 5 == 13, because in most C implementations, char is one byte.
source to share