What exactly is the purpose of the (asterisk) in pointers?
yes the asterisk *
have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like
int *ptr = 7;
its wrong(not an error) as pointersptr
expects valid address but you providedconstant(7)
. upto declaration it's okay but when you go for dereferencing it like*ptr
it gives problem because it doesn't know what is thatdata/value
at7 location
. So Its always advisable to assign pointers variable with valid addresses. for e.gint input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello";
=> here*
is just to inform the compiler thatptr
is apointer variable
not normal one &Hello
is achar array
i.e valid address, so this syntax is okay. Now you can doif(*ptr == 'H') { /*....*/ } else { /*.... */ }
*
has different meaning depending on the context.
Declaration of a pointer
int* ap; // It defines ap to be a pointer to an int. void foo(int* p); // Declares function foo. // foo expects a pointer to an int as an argument.
Dereference a pointer in an expression.
int i = 0; int* ap = &i; // ap points to i *ap = 10; // Indirectly sets the value of i to 10
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *
, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a
that points to what the value &a
is. Your talking about what is at the address of a
now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a
itself has a footprint in memory however (potentially smaller than a
itself) and when you cout<<p_a<<endl;
you will write whatever the pointer address is, not whats there. *p_a
however will be &a
.
p_a
is normally smaller than a
itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short
and short*
.
There are two ways to "look at" variable b
(that's what probably confuses most beginners):
You can consider
b
as a variable of typeint*
.You can consider
*b
as a variable of typeint
.Hence, some people would declare
int* b
, whereas others would declareint *b
.But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either
b
as a pointer to an integer value, or*b
as the actual pointed integer value.You can get (read) the pointed value:
int c = *b
.And you can set (write) the pointed value:
*b = 5
.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a
pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a
will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a
, but you should be very careful changing the value of *a
.