pointer in c

Pointer in C

Practice Questions:

1.What will be the output of the C program?

#include<stdio.h>
int main(){
int i = 3;
int *j;
j = &i;
j++;
printf("%d ",*j);
return 0;
}

2.What is the output of following program?

# include <stdio.h>
void fun(int x)
{
    x = 30;
}

int main()
{
  int y = 20;
  fun(y);
  printf("%d", y);
  return 0;
}

3. What will be the output of the C program?

#include<stdio.h>
int main()
{
char *ptr = "Pointer-to-String", i;
printf("%s", ++ptr);
return 0;
}

4.Output of following program?

# include <stdio.h>
void fun(int *ptr)
{
    *ptr = 30;
}

int main()
{
  int y = 20;
  fun(&y);
  printf("%d", y);

  return 0;
}

5.What will be the output of the C program?

#include<stdio.h>
int main()
{
char *str = "His";
int i;
for(i = 0; i < strlen(str); i++)
printf("%s", str++);
return 0;
}

6.Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes.
#include <stdio.h>

int main()
{
    int arri[] = {1, 2 ,3};
    int *ptri = arri;

    char arrc[] = {1, 2 ,3};
    char *ptrc = arrc;

    printf("sizeof arri[] = %d ", sizeof(arri));
    printf("sizeof ptri = %d ", sizeof(ptri));

    printf("sizeof arrc[] = %d ", sizeof(arrc));
    printf("sizeof ptrc = %d ", sizeof(ptrc));

    return 0;
}

7.What will be the output of the C program?

#include<stdio.h>
struct classroom
{
int students[7];
};
int main()
{
struct classroom cr = {2, 3, 5, 7, 11, 13};
int *ptr;
ptr = (int *)&cr;
printf("%d",*(ptr + 4));
return 0;
}

8. What will be the output of the C program?

#include<stdio.h>
int main(){
int *ptr;
*ptr = 5;
printf("%d" , *ptr);
return 0;
}

9.What will be the output of the C program?

#include<stdio.h>
int main(){
 char array[5] = "Knot", *ptr, i, *ptr1;
 ptr = &array[1];
 ptr1 = ptr + 3;
 *ptr1 = 101;
 for(i = 0; i < 4;i++)
 printf("%c", *ptr++);
 return 0;
}

10.Assume that float takes 4 bytes, predict the output of following program.

#include <stdio.h>
int main()
{
    float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
    float *ptr1 = &arr[0];
    float *ptr2 = ptr1 + 3;

    printf("%f ", *ptr2);
    printf("%d", ptr2 - ptr1);

   return 0;
}

11.What will be the output of the C program?

#include<stdio.h>
#include<string.h>
int main(){
register a = 1;
int far *ptr;
ptr = &a;
printf("%u",ptr);
return 0;
}

12.What will be the output of the C program?

#include<stdio.h>
int main()
{
char arr[10] = "Mango", *ptr;
ptr = (&arr[1]++);
printf("%s",ptr++);
return 0;
}

13.What will be the output of the C program?

#include<stdio.h>
#define NULL "error";
int main()
{
 char *ptr = NULL;
 printf("%s",ptr);
 return 0;
}

14.What will be the output of the C program?

#include<stdio.h>
int main(){
int a = 36;
int *ptr;
ptr = &a;
printf("%u %u", *&ptr , &*ptr);
return 0;
}

15. What will be the output of the C program?

#include<stdio.h>
int main(){
 char *cities[] = {"UAE", "Spain", "America"};
 int **i = &cities[0];
 int **j = &cities[1];
 int **k = &cities[2];
 printf("%c%c%c\n", **i,**j,**k);
 return 0;
}

16. Which type of pointer is a most convention way of storing raw address in c programming?

A. Void pointer

B. Null pointer

C. Integer pointer

D. Double pointer

17.What will be the output of the C program?

#include<stdio.h>
int main()
{
   int a;
   char *x;
   x = (char *) &a;
   a = 512;
   x[0] = 1;
   x[1] = 2;
   printf("%dn",a); 
   return 0;
}

18. What will be the output of the C program?

#include<stdio.h>
unsigned long int (*function())[5]{
static unsigned long int arr[5] = {2, 3, 5, 7, 11};
printf("%d", *arr);
return &arr;
}
int main(){
unsigned long int (*ptr)[5];
ptr = function();
printf("%d", *(*ptr + 4));
return 0;
}

19.Fill the question mark to get "5" as an output?

#include<stdio.h>
int main()
{
 int i = 5,*ptr;
 ptr= &i;
 void *vptr;
 vptr = &ptr;
 printf("\nValue of iptr = %d ", ?);
 return 0;
}

20.What will be the output of the C program?

#include<stdio.h>
int main(){
 int i = 5;
 void *vptr;
 vptr = &i;
 printf("\nValue of iptr = %d ", *vptr);
 return 0;
}

21.What will be the output of the C program?

#include<stdio.h>
int main(){
int num = 10;
printf("num = %d addresss of num = %u",num, &num);
num++;
printf("\n num = %d addresss of num = %u",num, &num);
return 0;
}

22.What will be the output of the C program?

#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
k++;
printf("%d ",**k);
return 0;
}

 23.What will be the output of the C program?

#include<stdio.h>
#include<string.h>
int main(){
char *ptr = "hello";
char a[22];
strcpy(a, "world");
printf("\n%s %s",ptr, a);
return 0;
}

24.What will be the output of the C program?

#include<stdio.h>
#include<string.h>
int main(){
char a = 30, b = 5;
char *p = &a, *q = &b;
printf("%d", p - q);
return 0;
}

25.What will be the output of the C program?

#include<stdio.h>
int main(){
int i = 25;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u ",k,*k,**k);
return 0;
}

26.What will be the output of the C program?

#include<stdio.h>
int main()
{
char *cities[] = {"Delhi", "London", "Sydney"};
int **i = &cities[0];
printf("%c\n", **i);
return 0;
}

27.What will be the output of the C program?

#include<stdio.h>
int main(){
int a = 130;
char *ptr;
ptr = (char *)&a;
printf("%d ",*ptr);
return 0;
}

28.What will be the output of the C program?

#include<stdio.h>
int main(){
int a = 25, b;
int *ptr, *ptr1;
ptr = &a;
ptr1 = &b;
b = 36;
printf("%d %d",*ptr, *ptr1);
return 0;
}

29.What will be the output of the C program?

#include<stdio.h>
int main(){
int *ptr, b;
b = sizeof(ptr);
printf("%d" , b);
return 0;
}

30.What will be the output of the C program?

#include<stdio.h>
int main(){
int *ptr = 2;
printf("%d", sizeof(ptr));
return 0;
}

31.What will be the output of the C program?

#include<stdio.h>
int main(){
int a, b, c;
char *p = 0;
int *q = 0;
double *r = 0;
a = (int)(p + 1);
b = (int)(q + 1);
c = (int)(r + 1);
printf("%d %d  %d",a, b, c);
return 0;
}

32.What will be the output of the C program?

int main()
{
 char *ptr = "GeeksQuiz";
 printf("%cn", *&*&*ptr);
 return 0;
}


33.What will be the output of the C program?

#include<stdio.h>
int main()
{
 int *iptr;
 int i, arr[2][2] = {10, 11, 12, 13};
 iptr = *arr ;
 printf("%d ", *(iptr+2));
 return 0;
}

34.What will be the output of the C program?

#include<stdio.h>
int main()
{
char *ptr;
char string[] = "learn C from 2braces.com";
ptr = string;
ptr += 6;
printf("%s",ptr);
return 0;
}

35. What will be the output of the C program?

#include<stdio.h>
int main()
{
const int a = 5;
const int *ptr;
ptr = &a;
*ptr = 10;
printf("%d\n", a);
return 0;
}

36.What will be the output of the C program?

#include<stdio.h>
int main(){
 char arr[15] = "pointer array";
 int *ptr;
 ptr = arr;
 printf("%c",ptr[1]);
 return 0;
}


37.What will be the output of the C program?

#include<stdio.h>
int main()
{
printf("%d", sizeof(void *));
return 0;
}

38.What will be the output of the C program?

#include<stdio.h>
void function(char**);
int main()
{
char *arr[] = { "ant", "bat", "cat", "dog", "egg", "fly" };
function(arr);
return 0;
}
void function(char **ptr)
{
char *ptr1;
ptr1 = (ptr += sizeof(int))[-2];
printf("%s\n", ptr1);
}

39.What will be the output of the C program?

#include<stdio.h>
int main()
{
struct node
{
int a, b, c;
};
struct node num = {3, 5, 6};
struct node *ptr = & num;
printf("%d\n", *((int*)ptr + 1 + (3-2)));
return 0;
}

40.What will be the output of the C program?

#include<stdio.h>
#include<string.h>
int main(){
char *ptr = "hello";
char a[22];
*ptr = "world";
printf("\n%s %s",ptr, a);
return 0;
}



Comments

Popular posts from this blog

C&C++ programming tricky questions

c programming