Click here to Skip to main content
15,915,764 members
Please Sign up or sign in to vote.
1.06/5 (6 votes)
See more:
What is the use of pointers?
Which things that can be done better with pointers than without?
And which things that can be done only with pointers?
Please give me a simple example of it.
Posted
Comments
Sergey Alexandrovich Kryukov 3-Apr-13 14:26pm    
This is the utterly ineffective way to learn the technology!
—SA

Pointers are probably the single most useful (and the most dangerous) language element in C.

What do they allow? They let you code without having to know the name of the variable that holds the data all the time. They let you allocate memory at run time, so that you have as many different copies of it as you need - without pointers you would have to define a fixed size array, and either allocate too much space (which is wasteful) or too little space (with is disastrous). They let you form linked lists, and structures that contain lists of different other structures, and to reference functions without knowing which function it is at run time.

They basically let you do things properly, and flexibly. Without them, it would be like a big think book without an index, contents or page numbers. the info is in there somewhere, but it's a real pain to find it!
 
Share this answer
 
A pointer is declared as :

<pointer type=""> *<pointer-name>
In the above declaration :

pointer-type : It specifies the type of pointer. It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store.
pointer-name : It can be any name specified by the user. Professionally, there are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’
An example of a pointer declaration can be :

char *chptr;
In the above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.

How to initialize a Pointer?

A pointer is initialized in the following way :

<pointer> =


OR

<pointer declaration="">
<name-of-pointer> =

Note that the type of variable above should be same as the pointer type.(Though this is not a strict rule but for beginners this should be kept in mind).

For example :

char ch = 'c';
char *chptr = &ch; //initialize

OR

char ch = 'c';
char *chptr;
chptr = &ch //initialize
In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’.

Note that the ‘&’ operator is used to access the address of any type of variable.

How to Use a Pointer?

A pointer can be used in two contexts.

Context 1: For accessing the address of the variable whose memory address the pointer stores.

Again consider the following code :

char ch = 'c';
char *chptr = &ch;
Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points. i.e. the value given by ‘chptr’ would be equal to ‘&ch’.

For example :

char *ptr = chptr;
The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.

Context 2: For accessing the value of the variable whose memory address the pointer stores.

Continuing with the piece of code used above :

char ch = 'c';
char t;
char *chptr = &ch;
t = *chptr;
We see that in the last line above, we have used ‘*’ before the name of the pointer. What does this asterisk operator do?

Well, this operator when applied to a pointer variable name(like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. Since ‘chptr’ holds the address of variable ‘ch’ and value of ‘ch’ is ‘c’, so ‘*chptr’ yeilds ‘c’.

When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.

An Example of C Pointers

Consider the following code :

CODE :

#include <stdio.h>

int main(void)
{
char ch = 'c';
char *chptr = &ch;

int i = 20;
int *intptr = &i;

float f = 1.20000;
float *fptr = &f;

char *ptr = "I am a string";

printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr);

return 0;
}
OUTPUT :

$ ./pointers

[c], [20], [1.200000], [I], [I am a string]
 
Share this answer
 
Comments
vishal2592 3-Apr-13 11:17am    
But what about the array in pointers?
gurunbr1 4-Apr-13 1:28am    
array is a group of objects with similar data types
Pointers are special variables that hold memory addresses. Read from the beginning to the end of one (at least) tutorials below.

A Beginner's Guide to Pointers[^]
http://www.cplusplus.com/doc/tutorial/pointers/[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Apr-13 14:27pm    
My 5, especially for the "from the beginning" part.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900