Pointer is a very nice and confusing topic in c programming language. It’s nothing but a data type in case of c language. Pointers are aptly named: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that the teller can quickly look it up. These numbers are like the memory addresses of variables. A pointer in the world of safety deposit boxes would simply be anything that stored the number of another safety deposit box. Perhaps you have a rich uncle who stored valuables in his safety deposit box, but decided to put the real location in another, smaller, safety deposit box that only stored a card with the number of the large box with the real jewelry. The safety deposit box with the card would be storing the location of another box; it would be equivalent to a pointer. In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables.
The cool thing is that once you can talk about the address of a variable, you'll then be able to go to that address and retrieve the data stored in it. If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data! Moreover, if you need more memory for your program, you can request more memory from the system--how do you get "back" that memory? The system tells you where it is located in memory; that is to say, you get a memory address back. And you need pointers to store the memory address.
A note about terms: the word pointer can refer either to a memory address itself, or to a variable that stores a memory address. Usually, the distinction isn't really that important: if you pass a pointer variable into a function, you're passing the value stored in the pointer--the memory address. When I want to talk about a memory address, I'll refer to it as a memory address; when I want a variable that stores a memory address, I'll call it a pointer. When a variable stores the address of another variable, I'll say that it is "pointing to" that variable.
Types of pointer
It’s having various types
1) Integet pointer
2) Character pointer
3) String pointer
4) Structure pointer
5) Pointer of union
6) Function pointer
7) File pointer
Pointers comes in two flavor typed and untyped. A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type. It is also called void pointer or null pointer
To declare a pointer you must put a * in front of its name.
Here is an examaple of how to declare a pointer to an integer and an untyped pointer:
int main()
{
int *p;
void *up;
return 0;
}
here up pointer’s data type is
void,So it is untype pointer.
You can put the address of an integer into a pointer to an integer by using the & operator to get the integer's address.
int main()
{
int i;
int *p;
i = 5;
p = &i;
return 0;
}
You can access the value of the integer that is being pointed to by dereferencing the pointer. The * is used to dereference a pointer. Changing the value pointed to by the integer will change the value of the integer.
int main()
{
int i,j;
int *p;
i = 5;
p = &i;
j = *p;
//j = i *p = 7;
//i = 7 return 0;
}
No comments:
Post a Comment