Wait... you said "memory address"?
The concept of pointer it's strongly linked to the concept of memory adress so we need to make clear few things first. Let's say that a program is a collection of variables and functions. Every time that your program runs, the computer assigns it a portion of memory for each variable it creates. The size of each the memory block depends on the type of the variable, in order to get the computer working and do not mistake variables between processes (running programs), each memory block is endowed with a memory address. A memory address is a number (an unsigned int, but also has hexadecimal representation, and a physical place where the information it's saved, the information must be physically somewhere!, but that's another story). Said that, let say that a pointer is a special type of variable that stores a memory address.
Creating a pointer
Each pointer must be declared using the type of the variable which stores its memory address, so, for store the address of a double variable, we must have a pointer of type double
Each pointer must be declared using the type of the variable which stores its memory address, so, for store the address of a double variable, we must have a pointer of type double
double variable;
double *variable_pointer;
variable_pointer=&variable;
So "variable_pointer" stores the address of "variable". An assignation as
int variable;Would lead to error in execution/compilation process as a result of type mismatch
double *variable_pointer;
variable_pointer=&variable;
Let's see some examples on C code
In C/C++, the adress of a variable can be obtained by using the ampersand operator, that is, &.
When you make an statement like
&variableWhat you actually get is the memory adress of "variable". This can be more clear by the next example code:
#include <stdio.h>
#include <stdlib.h>
int by_pointer(int *i)
{
printf("\nVariable value passed \'by pointer\' : %d \n",*i);
printf("Memory adress : %X \n\n",&*i);
return *i;
}
int by_value(int j)
{
printf("\nVariable value passed \'by value\' : %d \n",j);
printf("Memory adress : %X \n\n",&j);
return j;
}
int main(void)
{I've compiled that code, an then ran the program and the output was
int y;
printf("Write an integer number\?\n");
scanf("%d",&y);
by_value(y);
by_pointer(&y);
printf("Variable initial address %X \n\n",&y);
}
Write an integer number?
3
Variable value passed 'by value' : 3
Memory adress : C42DC23C
Variable value passed 'by pointer' : 3
Memory adress : C42DC25C
Variable initial address C42DC25C
As you see, when the variable it's created, takes the address (in this particular example. The addresses change each programm run) "C42DC25C". When you pass the y variable to by_value(), another variable is created, that's why the address "C42DC23C" is shown (instead of "C42DC25C" which is the original memory address ). When we call the function by_pointer(&y) passing y's address, we see that the functions returns exactly the same address as "y", so it don't creates a new varible. The function uses the existing variable, so memory is saved.
You can use pointers to modify the value of an external variable by passing the argument using pointers. Let me show you an example code:
You can use pointers to modify the value of an external variable by passing the argument using pointers. Let me show you an example code:
#include <stdio.h>So, when you run the program, you can get a result like
#include <stdlib.h>
void modify_by_pointer(int *i)
{
printf("Write an integer number\?\n");
scanf("%d",&*i);
}
int main(void)
{
int y;
modify_by_pointer(&y);
printf("\nThe value of y is %d\n",y);
}
Write an integer number?
4
The value of y is 4
That's cuz, passing the direcction to the function, it modifies the variable itself, instead of creating a copy of it,. When you use pointers to pass arguments to functions, it's known as pass by reference, that's how you will find on the literature
Further reading
A good webpage http://www.cplusplus.com/doc/tutorial/pointers/
Or this book Absolute C++ - 5th edition - Walter Savitch
You can try this one if you want
No hay comentarios.:
Publicar un comentario