Saturday, October 22, 2011

c Pointer basic Basics


C   Pointer Basics Technical Questions


Hi Blog readers,
I am sabari, computer science engineer. Here I would like to Share some of my Interview preparation Question you may worked out (compile and run) from these you will come to know some interesting facts on C Pointer Basics,
Basics
Pointer is a variable that holds another variable’s address.
Pointer Example
#include
#include
int main()
{
    int *p,a;
    float *p1,b;
    p=&a;
    p1=&b;
    a=10;
    //clrscr();
    printf("a=%d\n",a);
    printf("a=%d\n",*p);
    /* pointer size*/
    printf("integer pointer size is %d\n",sizeof(p));
    printf("integer size is %d\n",sizeof(a));
    printf("integer pointer size is %d\n",sizeof(p1));
    printf("Float size is %d\n",sizeof(b));

    //printf("welcome");
    return 0;
}
// Function Pointer
#include
#include
void main()
{
int *p; //pointer for a function
int *f();//function  pointer
int a,b,c;
printf("Enter the three numbers:");
scanf("%d %d %d",&a,&b,&c);
p=f(a,b,c);
clrscr();
//printf("a address is :%u",p);
printf("VALUE OF BIG :%d",*p);
getch();
}
int *f(int x,int y,int z)
{
  static int a;
  a=(x
 // a=a*b;
 // printf("a address is :%u",a);
return(&a);
}
String copy
#include
#include
char source1[25],source2[25];
int main()
{
char *sp1,*sp2;
int r;
printf("welcome TO strcompare:\n Enter the sourc1");
scanf("%s",source1);
sp1= source1;
sp2= source2;
while(*sp1)
{
    *sp2=*sp1;
    sp1++;
    sp2++;
}
printf("the copied string is:%s",source2);
return 0;
}

//String compare
#include
#include
char source1[25],source2[25];
int main()
{
char *sp1,*sp2;
int r,c;
clrscr();
printf("welcome TO strcompare:\n Enter the sourc1");
scanf("%s",&source1);
printf("\n Enter the sourc1");
scanf("%s",&source2);
sp1= source1;
sp2= source2;
while(*sp1!='\o')
{
    if(*sp2==*sp1)
    {
    sp1++;
    sp2++;
     c=1;
    }
   else
  {
  printf("the string is not equl");
  *sp1='\o';
  }
  }
  if(c==1)
  {
     printf("the  string is equal");
     }
return 0;
}
Visit and download contents at:

No comments:

Post a Comment