#Help using pointers!
24 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Pointers themselves are numbers, they are addresses in memory, they point to locations in memory. The value of a pointer could refer to the address it holds. When you do something like if(ptr == NULL) you're checking the value of the pointer
On the other hand if you do something like *ptr then you're working with the value it points to
;compile
#include <stdio.h>
int main() {
int x = 42;
int* ptr = &x;
printf("ptr: %p\n", ptr);
printf("*ptr: %d\n", *ptr);
}
ptr: 0x7fff6673c5b4
*ptr: 42
I understand that they are used for pointing to a memory address
they point to other objects like anintor whatever
HOW they are able to point is using memory address. Lets say there is int x = 10; in your code. Your processor stores x at memory location 0x1000
now you make a pointer to x by writing int* ptr = &x; . Now you created a pointer to x. This pointer now stores the value 0x1000 . This is how it knows where it is pointing to. There is no secret bonding betwee x and ptr . ptr simply stores the address of x as its own value.
Also do note that ptr is also a variable , so it also needs storage ,to store this 0x1000 somewhere. The ptr variable itself might be created at the memory location 0x2000 or wherever
@carmine lantern the best way to visualize pointers as a beginner is to think of them as their own types
here is an example
int is a datatype that holds numbers like int x = 1234;
double holds decimals like double x = 21.51;
and pointers are their own types that holds INTEGERS like PTR x = 0x1234; // any address (ADDRESSES ARE INTEGERS because ram goes from 0x0 all the way to 2gb, 3gb, 4294967296, etc gb.... There is no Negative Ram Address makes no sense
and & operator under the hood just replaces something with its address like
double x = 12.5; // the address of "x" for example is "0x1234"
PTR y = &x; // under the hood it just swaps it to `PTR y = 0x1234;`
im not good at explaining or teaching but that is as far as i can go
you might be curious and say, if that is the case, then why are we not doing PTR and doing int*, or double* instead
here is a simple explanation: imagine someone tells you to read a data from address 0x1234 and print the number
he will not provide you a variable where you can easily do an &var so compiler can replace it under the hood
so you will end up doing a PTR x = 0x1234
but now we have a problem, the compiler doesn't know what kind of data at address 0x1234 exists
that's where the idea of int* or double* comes from, the * is just a replacement for PTR to make the code clean, and the int or double just tells the PC to interpret the data that is located at that address as an INTEGER or FLOAT
example:
// Address 0x1234 at RAM has this DATA: 572.51
as you can see the data is a decimal number
but if you do
int* x = 0x1234;
print(x); // the computer will interpret it as INTEGER because you specified int before * AND PRINT only 572
// but if you do
double* x = 0x1234;
print(x); // Now the computer will interpret it as DECIMAL number and print the entire 572.51
Ofcourse i left out a lot of concepts to explain that, but the goal was to simplify it so beginners can understand it
now to answer your question
remember when I said PTR itself is a type, just like when you do int x = 1234 computer creates a variable x and stores 1234
when you do PTR y = 0x123; the computer creates a variable y and stores the address 0x123
Remember the address is just a Number
no different from integers
different names but the same thing
because ram goes from 0x0 all the way to 4GB all positive numbers
so address can't be negative numbers
im not good at explaining stuff but that is as far as i can simplify it
thx guys❤️