Sunday, July 12, 2009

Explain the concept of pointer with a C program.?

A pointer in C is a positive integer value that refers to an address in memory where data is stored.





I think a lot of the confusion that arises from pointers is: whats the difference between a pointer and a normal variable?





Good question :)





Lets take the example of an integer and a pointer that points to an integer.





int x; %26lt;-- integer


int *p; %26lt;--pointer to integer





In effect, the only difference between using an integer and a pointer to an integer, is that you have to "de-reference" the pointer.





x = 3;


*p=3;





For the pointer, de-referencing (*p) means here that you are assigning the value 3 to data that p points to. P will still have the same address it had before, but now the data at that address contains the value 3.





The reason why pointers are useful is because you can have multiple pointers pointing to the same address in memory. So if you change the data of one pointer, you change them all. Also, if you have a large amount of data chunked together, you can use a pointer as sort of a "handle" to that big briefcase of data.





In more modern programming languages like Java, the concept of pointers and memory addresses are hidden under a layer of abstraction. So you don't have to worry about them as much. But it is a good idea to gain an understanding how pointers work, because they are a fundamental building block of programming.





I hope that helps.


pw

Explain the concept of pointer with a C program.?
Pointers in C are variables used to store addresses. I don't have much experience in C, I only use pointers to create free memory in C++.

salary survey

No comments:

Post a Comment