A pointer refers to memory address for another value.
A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer (source: http://en.wikipedia.org/wiki/Pointer_(computer_programming) ).
Look at the following statement:
int number = 5;
An memory location is allocated to store an integer whose value is 5, and you can access it using the name number.
The value 5 is stored in this area.
The computer references the area using an address.
Let suppose the address is 101. We can say put value 5 into a room number 101;
Variables that can store addresses are called pointers.
The following code defines a pointer pnumber
that contains the address of another variable, called number,
which is an integer variable containing the value 5.
The value that's stored in pnumber is the address of the first byte of number.
The word pointer is used to refer to just an address.
int number = 5; int *pnumber = &number;
A pointer to a value of type char is pointing to a value occupying 1 byte, whereas a pointer to a value of type long is usually pointing to the first byte of a value occupying 4 bytes.
Every pointer will be associated with a specific variable type, and it can be used only to point to variables of that type.
So pointers of type "pointer to int" can point only to variables of type int, pointers of type "pointer to float" can point only to variables of type float, and so on.
In general a pointer of a given type is written type* for any given type name type.
The type name void means absence of any type, so a pointer of type void* can contain the address of a data item of any type.
Type void* is often used as a parameter type or return value type with functions that deal with data in a type-independent way.
Any kind of pointer can be passed around as a value of type void* and then cast to the appropriate type when using it.
The address of a variable of type int can be stored in a pointer variable of type void*, for example.
When you want to access the integer value at the address stored in the void* pointer, you must first cast the pointer to type int*.
You can declare a pointer to a variable of type int with the following statement:
int *pnumber;
The type of the variable with the name pnumber is int*.
It can store the address of any variable of type int.
Note that you can also write the statement like this:
int* pnumber;
The statement just creates the pnumber variable but doesn't initialize it.
You can initialize pnumber so that it doesn't point to anything by rewriting the declaration like this:
int *pnumber = NULL;
NULL is a constant that's defined in the standard library and is the equivalent of zero for a pointer.
NULL is a value that's not pointing to any location in memory.
If you want to initialize your variable pnumber with the address of a variable you've already declared, you use the address of operator, &:
int number = 5; int *pnumber = &number;
Now the initial value of pnumber is the address of the variable number.
You can declare regular variables and pointers in the same statement, for example:
double value, *pVal, fnum;
This statement declares two double-precision floating-point variables, value and fnum, and a variable pVal of type "pointer to double."
To obtain memory address of a variable, we can use & syntax.
#include <stdio.h>
//from w w w . j ava2 s . c o m
int main(int argc, const char* argv[]) {
int n;
n = 10;
printf("value n: %d \n",n);
printf("address n: %x \n",&n);
return 0;
}
The code above generates the following result.
To declare a pointer of a specific data type, we can use *
syntax.
This variable consists of memory address of our pointer variable.
#include <stdio.h>
/*www . j a v a2 s.c o m*/
int main(int argc, const char* argv[]) {
int n;
int* nPtr;
n = 10;
nPtr = &n;
printf("value n: %d \n",n);
printf("address n: %x \n",(unsigned int)&n);
printf("value nPtr: %x \n",(unsigned int)nPtr);
printf("address nPtr: %x \n",(unsigned int)&nPtr);
printf("value pointer nPtr: %d \n",*nPtr);
return 0;
}
You can see a value of nPtr is memory address of variable n.
If n is set value 10, then value of pointer nPtr which we declare as *nPtr is the same value with value of variable n.
The code above generates the following result.
You use the indirection operator, *, to access the value of the variable pointed to by a pointer.
This operator is also referred to as the dereference operator because you use it to "dereference" a pointer.
Suppose you declare the following variables:
int number = 15; int *pointer = &number; int result = 0;
The pointer variable contains the address of the variable number, so you can use this in an expression to calculate a new value for result, like this:
result = *pointer + 5;
The expression *pointer will evaluate to the value stored at the address contained in the pointer.
This is the value stored in number, 15, so result will be set to 15 + 5, which is 20.
The follow code declares a variable and a pointer and then output their addresses and the values they contain.
#include <stdio.h>
/*from ww w.j a v a2 s .c om*/
int main(void)
{
int number = 0; // A variable of type int initialized to 0
int *pnumber = NULL; // A pointer that can point to type int
number = 5;
printf("number's address: %p\n", &number); // Output the address
printf("number's value: %d\n\n", number); // Output the value
pnumber = &number; // Store the address of number in pnumber
printf("pnumber's address: %p\n", (void*)&pnumber); // Output the address
printf("pnumber's size: %zd bytes\n", sizeof(pnumber)); // Output the size
printf("pnumber's value: %p\n", pnumber); // Output the value (an address)
printf("value pointed to: %d\n", *pnumber); // Value at the address
return 0;
}
The code above generates the following result.
Because you can access the contents of number through the pointer pnumber, you can use a dereferenced pointer to a numerical type in arithmetic statements.
For example:
*pnumber += 5;
This statement increments the value of whatever variable pnumber currently addresses by 5.
The * indicates you're accessing the contents to which the variable called pnumber is pointing.
The variable pnumber can store the address of any variable of type int.
This means you can change the variable that pnumber points to like this:
int value = 9; pnumber = &value;
Suppose you repeat the same statement that you used previously:
*pnumber += 5;
The statement will operate with the new variable, value, so the new contents of value will be 14.
A pointer can contain the address of any variable of the appropriate type, so you can use one pointer variable to change the values of many different variables, as long as they're of a type compatible with the pointer type.
This example uses pointers to modify values stored in some other variables.
#include <stdio.h>
/*w ww . j a va 2 s .c om*/
int main(void)
{
long num1 = 1L;
long num2 = 1L;
long *pnum = NULL;
pnum = &num1; // Get address of num1
*pnum = 2L; // Set num1 to 2
++num2; // Increment num2
num2 += *pnum; // Add num1 to num2
printf("num1 = %ld num2 = %ld *pnum = %ld *pnum + num2 = %ld\n",
num1, num2, *pnum, *pnum + num2);
return 0;
}
The code above generates the following result.
The following code shows how to use pointer variable in scanf
function.
When you've used scanf() to input values, you've used the & operator to obtain the address of the variable that is to receive the input.
When you have a pointer, you can use the pointer name as an argument.
#include <stdio.h>
/* ww w.ja v a 2s . co m*/
int main(void)
{
int value = 0;
int *pvalue = &value; // Set pointer to refer to value
printf ("Input an integer: ");
scanf(" %d", pvalue); // Read into value via the pointer
printf("You entered %d.\n", value); // Output the value entered
return 0;
}
The code above generates the following result.