[go: up one dir, main page]

0% found this document useful (0 votes)
190 views7 pages

Pointers in C-Que Set

The document discusses pointers in C programs. It provides 8 code examples with questions about the output of each example. The examples cover various pointer operations and declarations including dereferencing pointers, pointer arithmetic, passing pointers to functions, pointer to pointer, pointer to array, void pointers, and more. Determining the output of each example requires understanding how pointers work in C and how they are used in the given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views7 pages

Pointers in C-Que Set

The document discusses pointers in C programs. It provides 8 code examples with questions about the output of each example. The examples cover various pointer operations and declarations including dereferencing pointers, pointer arithmetic, passing pointers to functions, pointer to pointer, pointer to array, void pointers, and more. Determining the output of each example requires understanding how pointers work in C and how they are used in the given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

POINTERS IN C-PROGRAMS

1.What will be the output? 2. What will be the output?


#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a[] = { 1, 2, 3, 4, 5} ; int a = 5;
int *ptr; int *ptr ;
ptr = a; ptr = &a;
printf(" %d ", *( ptr + 1) ); *ptr = *ptr * 3;
printf("%d", a);
return 0;
} return 0;
}

3.What will be the output? 4.What will be the output?


#include<stdio.h>
#include<stdio.h>
int main()
int main()
{
{
int x = 20, *y, *z;
int i = 6, *j, k;
// Assume address of x is 500 and
j = &i;
// integer is 4 byte size
printf("%d\n", i * *j * i + *j);
y = &x;
return 0;
z = y;
}
*y++;
*z++;
x++;
printf("x = %d, y = %d, z = %d \n", x, y, z);
return 0;
}
5.What will be the output? 6. What will be the output of following
#include<stdio.h> program?
int main() #include<stdio.h>
{ int main()
int x = 10; {
int *y, **z; int a, b;
y = &x; if(scanf("%d%d", &a, &b)==2)
z = &y; printf("true");
printf("x = %d, y = %d, z = %d\n", x, *y, else
**z); printf("false");
return 0; return 0;
} }
7. What will be the output of following 8. What will be the output of following
program? program?
#include<stdio.h> #include<stdio.h>
int main() int ar[] = {18, 23, 45, 56, 4, 6, 45};
{ int main()
int a=0; {
a=a++ + a++ - a++ + ++a; int i, x;
printf("%d\n", a); x = (sizeof(ar)/sizeof(ar[0]));
return 0;
} for (i=-1; i<=(x-2); i++)
printf("%d, ", ar[i+1]);
}
9. What will be the output of following 10. What will be the output of following
program? program?
#include<stdio.h> #include<stdio.h>
int count; int main()
void display(); {
int main() int flag = 0, flag1 = 0, n;
{ char s;
count = 1; flag = scanf("%d", &n);
printf("Value of count is %d, ", count); flag1 = scanf("%d", &s);
display(); printf("Value of flag is %d", flag);
} printf("\nValue of flag is %d", flag1);
void display() return 0;
{ }
extern int count;
printf("\nValue of count is %d", count);
}
11. What will be the output of following 12. What will be the output of following program?
#include <stdio.h>
program?
int main()
#include<stdio.h> {
int *ptr;
union student
int x;
{ ptr = &x;
*ptr = 0;
int y[34];
char var2[7]; printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
char arr[8];
*ptr += 5;
char ch[5]; printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
};
(*ptr)++;
int main() printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
{
return 0;
union student st; }
printf("%d", sizeof(union student));
(A) x = 0
return 0; *ptr = 0
x=5
}
*ptr = 5
x=6
*ptr = 6
13.What will be the output of the program ?

(B) x = garbage value


#include<stdio.h>
*ptr = 0
int main() x = garbage value
*ptr = 5
{
x = garbage value
int i=3, *j, k; *ptr = 6
j = &i;
(C) x = 0
printf("%d\n", i**j*i+*j);
*ptr = 0
return 0; x=5
*ptr = 5
}
x = garbage value
*ptr = garbage value

(D) x = 0
*ptr = 0
x=0
*ptr = 0
x=0
*ptr = 0
14. What would be the output of the 15. Would the following code compile
following program? successfully?
main()
{ main()
char far *a=0x00000120; {
char far *b=0x00100020; Printf(“%c”,7[‘Sundaram’]);
char far *c=0x00120000; }
if(a==b)
printf(“\nHello”); a) Yes it will print sundara.
if(a==c) b) No it will show compilation error.
printf(“\nHi”); c) Yes print m of sundaram.
if(b==c) d) None
printf(“\nHello Hi”);
if(a>b && a>c&& b>c)
printf(“\nBye”);
}
16. In the following program how 17.What would be the output of the
would you print 50 using p? following program?

main() main()
{
{ Int arr[]={12, 13, 14, 15, 16};
Printf(“\n%d %d %d”, sizeof(arr),
Int a[]={10,20,30,40,50};
sizeof(*arr), sizeof(arr[0]));
char *p; }

p = (char *)a a) 12 13 14 16
} b) 10 12 15 16
c) 10 2 2
d) None

a) printf(“\n%c”, ((int *)p+4));


b) printf(“\n%d”,(*(int *)p+4));
c) printf(“\n%d”,*((int *)p+4));
d) None.
18. Would the following program 20. What is the output of this C code?

compile? int main()


main() {
{ char *p = NULL;
int a=10, *j; char *q = 0;
void *k; if (p)
J = k = &a; printf(" p ");
J++; else
K++; printf("nullp");
printf(“\n%u %u”, j, k); if (q)
}
printf("q\n");
else
a) Yes, there is no problem in the
printf(" nullq\n");
code.
}
b) Yes, but void *k is not correct.
A. nullp nullq
c) No, an error on the K++ statement
B. Depends on the compiler
d) No
c. p q
d. x nullq where x can be p or nullp depending
on the value of NULL

21. What is the output of this C code? 22.What will be the output of following
int main() program ?
{ #include <stdio.h>
int *ptr, a = 10; int main()
ptr = &a; {
*ptr += 1; char *str
printf("%d,%d/n", *ptr, a); []={"AAAAA","BBBBB","CCCCC","DDDD
} D"};
A. 10,10 B. 10,11 C. 11,11 D. 11,10 char **sptr []={str+3,str+2,str+1,str};

23.What will be the output of char ***pp;

following program ? pp=sptr;


++pp;
#include <stdio.h> printf("%s",**++pp+2);
char* strFun(void) return 0;
{ }
char *str="IncludeHelp"; BBBBB CCCCC BBB Error
return str; Answer
} Correct Answer - 3
int main() BBB
{
*str is a array pointer of string, **sptr is array
char *x;
pointer(double pointer) that is pointing to str
x=strFun();
strings in reverse order. ***pp also a pointer
printf("str value = %s",x);
that is pointing sptr base address.
return 0;
++pp will point to 1st index of sptr that contain
}
str value = Garbage value str+2 ("CCCCC").

str value = IncludeHelp in printf("%s",**++pp+2); ++pp will point to


Error str+1, and **++pp, value stored @ str+1
No output ("BBBBB).
Answer str value = IncludeHelp and (**++pp)+2 will point the 2nd index of
"BBBBB", hence BBB will print.

You might also like