Monday, July 27, 2009

Can someone give an example of a source code for C which shows the advantage of using pointers?

Im kinda a newbie in C... Why not rather use variables? Why there are some cases you need to use Pointers?


Please explain its advantage by giving an example of a code for it...


Thanks, I really need help... (^^,)

Can someone give an example of a source code for C which shows the advantage of using pointers?
Pointers have many advantages over normal variables. There are many senarios that require the use of pointer and not the normal variable. I won't give you a code coz it'll take up a lot of time and space, but i'll present some senarios.


1%26gt; let's say you want to return 3 diffrent variables from a function. a function can only return only one variable. so, to do this you have to send pointers of three variables to the function as paramater. if you send them as plain vars then the values chaged inside a function gets lost when the function returns.


2%26gt; you can use pointers when you need to manipulate different memory locations through same variable.


3%26gt; you can have pointers that points to a function instead of a variable. these are very useful when designing graphic user interface (GUI).


4%26gt; when you need to allocate momory during runtime, by using malloc() or calloc(), you must do it through pointers.


5%26gt; another important use of pointer is that, if your program requires very large amount of memory, then you have to allocate memory from heap memory instead of stack memory. this also requires dynamic memory allocation through pointers.





Note: since you're a beginner in c programming, you might not understand everything i talked about above. don't worry. continue using simple variables where you can. pointers can give you a lot of trouble if you're inexperienced. as you go on programming you'll encounter situations where you know you have to use pointers. for now, just understand what they are, and don't get too eager to use them. there will be a time when you can't work without them. trust me ;)
Reply:i see your e-mail is listed in your profile. so if got some time i will send you some examples, but you might have to wait for a while. Report It

Reply:Here you go:





#include "stdio.h"





void main()


{


printf( "A pointer can be allocated dynamically." ) ;


printf( "A pointer can be deallocated dynamically !" ) ;


printf( "A pointer can be set to NULL" ) ;


printf( "A pointer can be used to point to different objects." ) ;


printf( "A pointer supports pointer arithmetic." ) ;


printf( "A pointer makes it possible to have a concept like void*" ) ;


printf( "A pointer can even point to a function !!" ) ;


printf( "The previous point allows us to implement polymorphism in C although language doesn't support it." ) ;


printf( "A pointer must be used if a change in function param's has to be reflected in caller function." ) ;


printf( "\n" ) ;


printf( "Without pointers we're lost." ) ;


printf( "Thank you for your time. Please ask more questions if you need examples for each one of these advantage." ) ;


}


No comments:

Post a Comment