Monday, July 27, 2009

Tell me any program in c which uses pointers?

pointers are rich feature of C which make it different from others programming Languages.


pointer is just a variable which is capable to hold the address of corresponding variable.


e.g.--


int i=7;


int *j;


j=%26amp;i;


here j will keep the address of i,and *j will keep value at address of j i.e.7


let see a programm which does not using pointers---





void fun(int);


void main()


{


int i=7;


fun(i);


printf("%d",i);


getch();


}


void fun(int i)


{


i=i*9;


getch();


}





here output will be 7! . bcoz we are passing value of i as photo copy which can not be changed in other function.here if you want to change the value of i then u must be pass it by reffrence,then it will needed pointer.look how---





void fun(int *);//since we will pass value of i as reff


void main()


{


int i=7;


fun(%26amp;i);//passing address of i;


printf("%d",i);//it will now print 63,since i got changed in following function


getch();


}


void fun(int *i)//to hold the address of i ,here must be a pointer


{


// *i means value value at address of i means 7.


*i=*i*9;//it will be 63


getch();


}

Tell me any program in c which uses pointers?
Have you ever played a Strategy Game such as AOE, RedAlert or Craft family game. When you use your mouse click at the soldier you want, you know its power, its weapons and its information. How to do like that in your program?... See the code.





struct SOLDIER


{


int x;


int y;


int power;


char name[20];


char weaponlist[20];


int currentweapon;


};





SOLDIER s1, s2, s3, s4;





Now you have 4 soldiers in your game. What’s next. Imaging, now your game is running and there are 4 soldiers around the game’s ground. You want to control the first soldier. You must have Pointer like this.





SOLDIER *p;





And use “p” points to the address of which to be controlled, use this code.





p=%26amp;s1;


p-%26gt;x=100;


p-%26gt;y=200;





Now, you can control the first soldier by using “p”. Let’s see it out. You didn’t touch the variable “s1” but you can control “s2” by pointer. Think about your Strategy Game, RTS game and many games that you can use mouse for clicking and dragging and dropping items on the game. How do we do? Yes, of course; Pointer is the answer.





About example I mentioned is “struct”. In the real game, you may change to “class” because within class, you can have public and private function that can make your structure more flexible.





By the way, this is just an example of how to use pointer in real life. I have many more examples such as… data structure built-in application.





ASTALAVISTA
Reply:There are many programs which uses pointers.





Pointers are widely used to allocate memory dynamically using (malloc() and free()).





The below example uses a pointer to get a string from user. If we use just a array of character we have to specify its size. If we specify too much it is a waste. If we specify less it will become useless.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


char *p;


printf("\n\n Enter a string : ");


scanf("%s",p);





printf("\n\n The string is %s",p);


getch();


}





Then, my favorite, the swap program. It is an example of how pointers in a function can affect the original variable.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void swap(int*,int*);





void main()


{


int a,b;





clrscr();


printf("\n\n Enter two numbers,\n\n num 1: ");


scanf("%d",%26amp;a);





printf(" num 2: ");


scanf("%d",%26amp;b);





printf("\n The numbers before swap are,\tnum1= %d \tnum2= %d",a,b);





swap(%26amp;a,%26amp;b);





printf("\n The numbers after swap are,\tnum1= %d \tnum2= %d",a,b);


getch();


}





void swap(int *a,int *b)


{


int temp;


temp=*a;


*a=*b;


*b=temp;


}





Next, the file pointer. Used to read,write in a file or any stream.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


FILE *fp;


char name[50];


int n,i;





clrscr();


printf("\n Enter your name: ");


gets(name);





printf("\n How many time you want to display: ");


scanf("%d",%26amp;n);





fp=fopen("files.txt","w");


for(i=0;i%26lt;n;i++)


fprintf(fp,"%s\n",name);


fclose(fp);





printf("\n please open files.txt in the present directory.");


getch();


}





Afterthat, to allocate memory using new keyword.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


void *ptr;


ptr= new int;


*ptr=5;





clrscr();


printf("\n\n The number is %d",*ptr);


getch();


}





And pointers dont end this way. They lead to the usage of linked list, stacks , queues and binary trees.


These are the advancements to represent data.





If you have any problem compiling the source codes, call me(m_gopi_m@yahoo.co.in)
Reply:Almost all but the smallest simple C programs use pointers.


Command line arguments are passed as pointers.


Function calls, such as printf("Hello World\n"), that involve strings are passed as pointers.
Reply:To swap two numbers pointers are used and mostly all simple programs use pointers to quick reference of memory
Reply:/*to create a stack */


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


int *top;


int stack[50];


void main()


{


int i,x,b;


int pop();


top=%26amp;stack[0];


printf("the pushed elements are :");


for(i=0;i%26lt;=10;i++)


{


scanf("%d",%26amp;b);


push(b);


}


printf("the poped elements are :\n");


while(top != stack)


printf("%d\n",pop());


}


push(int x)


{


*top=x;


top++;


}


pop()


{


int x;


top--;


x= *top;


return(x);


}


No comments:

Post a Comment