Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the real usage of pointers in C?

Status
Not open for further replies.

params

Programmer
Oct 17, 2001
3
SG
Hi Guys!!!
I am a new joinee in the forum.....basically a Java programmer...Just got interested in C language and have been working in the same since a few days before...
While working with C Pointers(I know its used for accessing the address of a specific variable.)I suddenly thought at its real-time usage....
My question is whats the real usage of pointers and what is the advantage of having pointers in C?Is it only the fact that it can save a lot of time and efforts in programming?..or we can achieve some special things using pointers which the languages not having pointers cannot do?
 
benefits of pointers:

1. Allows dynamic memory allocation - can declare a pointer at compile time, obtain the size of an array from the user at run time, then allocate the correct memory using malloc.

2. Allows you to pass function arguments by reference - avoids putting huge structures on the stack, allows the function to change the values of the structure directly.

3. Allows very fast iteration through arrays - low level routines can iterate through arrays using a *p++ construct which is very fast.

4. You can also assign a pointer to a function *f(), which you can then assign to the appropriate function based on some input data characteristics at run time. For example one function might run for real numbers, another function might run for complex numbers; at run time you can let f = real() or complex() (this is done using virtual classes and inheritance in C++).
 
One more:

5) Allows you access to the registers of a memory mapped device or ability to attach to a segment of shared memory.
 
Guys,
I am very glad to receive your replies....Thanks for the same....
I do have a clarification to make.....Can we write a particular variable to a specific memory location(address) in the RAM? If so what is the practical usage of this?
What if there is an address conflict with another application/device using that area....



 
You can write some data in a specific portion of the memory, but that portion should be allocated by you(dynamic memory manipulation) or allocated for your variable or the system should have rights to write (like the memory area for the dispaly devices).

Writing to display momory area directly will give us the fast display.

without these, writing some data directly into memory will give lot of problems.

Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top