5 Pointers
5 Pointers
5 Pointers
-------------------------------------------------------------------------------------------------------
int main()
int *p = 10;
printf(“ %u\n”, (unsigned int)p); //print the 10 but next line it will give an error
printf(“%d\n”,*p);
2) #include <stdio.h>
int main()
int main()
int x = -300;
p = &x;
printf(“%d\n”,*p++); //212
printf(“%d\n”,*p);//254
4) #include<stdio.h>
int main()
int x = 256;
char *p = &x;
5) #include<stdio.h>
int main()
int x = 300;
if(*(char *)&x == 44) // it will take just first byte (0010 1100)=44
else
printf(“Big Endian\n”);
6) #include <stdio.h>
void main()
int x = 0;
int *ptr = &5; // we can not assign a const add it will give lvalue error
printf("%p\n", ptr);
7) #include<stdio.h>
int main()
int const *p = 5;
int q;
p = &q;
printf(“%d”,++(*p)); //we can not modify the *p we use int const *p thats why
8) #include<stdio.h>
int main()
int x = 10;
int const * const p; //at the declaration time we have to give the address other wise it will
not accept the address also and we can not modify the data also
p = &x; //we can not assign the address because it is in read only coundition
printf(“%d\n”, *p);
9) #include <stdio.h>
int x = 0;
void main()
printf("%p\n", ptr);
ptr++; // read only coundition
int main()
int *p;
*p = 5; //modifying the 3 to 5
printf("%d\n", ary[3]);
int main()
void main()
int main()
void *p;
14) #include<stdio.h>
int main()
int a[ ] = {10,20,30,40,50},i;
char *p = a;
for(i=0;i<5;i++)
15) #include<stdio.h>
int main()
int a[]={10,20,30,40,50};
char *p;
p=(char *)a;
printf("%d\n",*((int *)p+4)); // just print 50 because ‘a’ stores base add. And in that
explicite type conversion so int+4=last add. That’s why
int main()
int main()
int i = 10;
void *p = &i;
printf("%d\n", (int *)*p); // we can not use such type of statement *p is wrong for printing
the void pointer
return 0;
int main()
int n = 1;
printf("%d\n", n);//4
int main()
a) a + b
c) a * b
d) a / b
Ans: b)
a) a | b
b) a ^ b
c) a & b
Ans: d)
void main()
char *s = "hello";
char *n = "cjn";
int i = 0;
void main()
int a[5] = {6, 5, 3}; // partially initialize array so it will give other value are zero
m(&a);//calling function
24)#include <stdio.h>
void foo(int*);
int main()
foo(p++);
foo(p);
printf("%d\n", *p);
25)#include <stdio.h>
int main()
int j = 2;
p = &j;
26) #include<stdio.h>
int main()
{
const int ary[4] = {1,2,3,4}; // we can just read only we cannot modify the array
int *p = ary+3;
*p = 5;
ary[3] = 6;
printf(“%d”,ary[3]);
27) #include<stdio.h>
int main()
while(*p!='\0');
++*p++;
printf(“%s %s\n”,p,p1);
28) #include<stdio.h>
int main()
char *x = “VECTOR”;
29) #include<stdio.h>
int main()
char a[ ] = “abcdefgh”;
int *ptr = a;
printf(“%x %x\n”,ptr[0],ptr[1]);
30) #include<stdio.h>
#include<string.h>
int main()
strcpy(strc, str);
printf("%s\n", strc);
return 0;
31)#include <stdio.h>
int main()
return 0;
}
32) #include <stdio.h>
int main()
str[5] = '.';
printf("%s\n", str);
return 0;
int main()
return 0;
int main()
char strary[] = "hello world"; // it is char array and the given array is string \0 is added
automatically size is 12
return 0;
return 0;
36) #include<stdio.h>
int main()
*(a>b ? &a : &b) = (a+b)>c; // a+b>c is false so it return 0 and it is stored in the a address
means a modify
37) Find the sizeof any datatype with out using sizeof operator. (Hint : Use pointers)
38) #include<stdio.h>
int main()
int i;
double a = 5.2;
char *ptr;
for(i=0;i<=7;i++)
printf(“%d\n”,*ptr++);
return 0;
}
#include<stdio.h>
int main()
int **ptr;
int a = 129;
p = &a;
ptr = &p;
printf(“ p = %d p = %u &p = %u\n”, *p, p, &p); //other is we can change *p to (int *)p is
correct
40) #include<stdio.h>
main()
char a[20];
char *p,*q;
p=&a[0];
q=&a[10];
printf("%d %d\n",q-p,&q-&p);
41) #include<stdio.h>
main()
int a=0x12345678;
void *ptr;
ptr=&a;
printf("0x%x\n",*(int *)&*&*(char*)ptr);
42) #include<stdio.h>
main()
int a[5]={1,2,3,4,5};
printf("%d %d\n",*(a+1),*(ptr-1));
printf("%d %d\n",*(a+1),*(ptr));
void main()
char *p = s;
printf("%c\t%c", 1[p], s[1]); // we can use this 1[p] it will print the 1st block of the array
44) #include<stdio.h>
main()
char a[]="abcde";
char *p=a;
p++; // address is increment by 1 byte
p[2]='z'; // here p is changing the value of e because it will change the 2nd position
45) #include<stdio.h>
main()
{
char a[]=”ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
int i,*p = a;
for(i=0;i<5;i++)
printf(“%d\t”,*p++);
46) #include<stdio.h>
main()
char a[]=”abcdef”;
char *ptr1 = a;
ptr1 = ptr1+(strlen(ptr1)-1);
printf(“%c”, --*ptr1--);
printf(“%c”,--*--ptr1);
printf(“%c”,--*(ptr1--));
printf(“%c”,--*(--ptr1));
printf(“%c”,*ptr1);
47) #include<stdio.h>
char *str1 = “Hello”;
char *str3;
str3 = strcat(str1,str2);
printf(“%s %s\n”,str3,str1);
int main()
return 0;
48) #include<stdio.h>
int main()
char a[]=”Hello”;
char *p=”Hai”;
a=”Hai”;
p=”Hello”;
printf(“%s %s\n”,a,p);
return 0;
49) #include<stdio.h>
int main()
int i,n;
char *x=”Alice”;
n=strlen(x);
*x=x[n];
for(i=0;i<=n;i++)
printf(“%s”,x);
x++;
printf(“%s\n”,x);
return 0;
50) #include<stdio.h>
int main()
printf(str,34,str,34);
return 0;
k++;
k[2] = 'm';
printf("%c\n", *k);
void main()
{
char s[] = "hello";
f(s);
printf("%s\n",s);
52) #include<stdio.h>
main()
char *p;
p = “abcder”;
t1(p);
{
if(*q!='r')
putchar(*q);
t1(q++);
53) #include<stdio.h>
int main()
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
return 0;
int main()
foo(ary);
int i = 10;
p = &i;
void main()
int k = 5;
int *p = &k;
**m = 10;
int main()
int a = 1, b = 2, c = 3;
printf("%d ",**sptr);
*sptr = ptr2;
printf(("%d ",**sptr);
void main()
int *p = a;
int *r = &p;
printf("%d\n", (**r));
int main()
return 0;
int j = 2;
*p = &j;
int main()
int i = 11;
int *p = &i;
foo(&p);
int j = 10;
*p = &j;
int main()
int i = 10;
int *p = &i;
foo(&p);
int j = 11;
*p = &j;
int *f();
int main()
int *p = f();
printf("%d\n", *p);
int *f()
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}
62) #include <stdio.h>
void main()
int i = 0;
void main()
int i = 0, j = 0;
void main()
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[1]));
int main()
return 0;
int main()
return 0;
while (argc--)
printf("%s\n", argv[argc]);
return 0;
printf("%s\n", *argv);
return 0;
printf("%s\n", *(argv++));
return 0;
71) #include<stdio.h>
while(sizeofargv)
printf(“%s ”,argv[--sizeofargv]);
return 0;
int main()
char *str[]={“Progs”,”Do”,”Not”,”Die”,”They”,”Croak!”};
printf(“%d %d”,sizeof(str),strlen(str[0]));
return 0;
73) #include<stdio.h>
int main()
{
static char *s[]={“black”,”white”,”pink”,”violet”};
char **ptr[]={s+3,s+2,s+1,s},***p;
p = ptr;
printf(“%s\n”,**p+1);
return 0;
74) #include<stdio.h>
main()
char *m[]={“jan”,”feb”,”mar”};
printf(“%s\t”,m[1]);
printf(“%s\t”,d[1]);
75) #include<stdio.h>
char *argv[]={“ab”,”cd”,”ef”,”gh”};
fun(argv);
return 0;
char *t;
t=(p+=sizeof(int))[-1];
printf(“%s\n”,t);
void first()
printf("first");
void second()
first();
void third()
second();
void main()
{
void (*ptr)();
ptr = third;
ptr();
return a + b;
int main()
fn_ptr = add;
return a * b * c;
void main()
function_pointer = mul;
}
79) #include<stdio.h>
int main()
fun(main);
printf(“Hi\n”);
return 0;
printf(“Hello\n”);
return 0;
80) #include<stdio.h>
int main()
printf(p);
A.Tandava Ramakrishna
Email:ramakrishna@vectorindia.org