C Advanced Pointer

Question 1

C

void fun(int *p) 
{ 
  int q = 10; 
  p = &q; 
}     
  
int main() 
{ 
  int r = 20; 
  int *p = &r; 
  fun(p); 
  printf("%d", *p); 
  return 0; 
}
Cross

10

Tick

20

Cross

Compiler error

Cross

Runtime Error



Question 1-Explanation: 

Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains uneffected. If we want to change a local pointer of one function inside another function, then we must pass pointer to the pointer. By passing the pointer to the pointer, we can change pointer to point to something else. See the following program as an example.

void fun(int **pptr)
{
  static int q = 10;
  *pptr = &q;
}

int main()
{
  int r = 20;
  int *p = &r;
  fun(&p);
  printf(\"%d\", *p);
  return 0;
}

In the above example, the function fun() expects a double pointer (pointer to a pointer to an integer). Fun() modifies the value at address pptr.  The value at address pptr is pointer p as we pass address of p to fun().  In fun(), value at pptr is changed to address of q.  Therefore, pointer p of main() is changed to point to a new variable q. Also, note that the program won’t cause any out of scope problem because q is a static variable. Static variables exist in memory even after functions return. For an auto variable, we might have seen some unexpected output because auto variable may not exist in memory after functions return.

Question 2
Assume sizeof an integer and a pointer is 4 byte. Output?
#include <stdio.h>

#define R 10
#define C 20

int main()
{
   int (*p)[R][C];
   printf("%d",  sizeof(*p));
   getchar();
   return 0;
}
Cross
200
Cross
4
Tick
800
Cross
80


Question 2-Explanation: 
Output is 10*20*sizeof(int) which is “800″ for compilers with integer size as 4 bytes. When a pointer is de-referenced using *, it yields type of the object being pointed. In the present case, it is an array of array of integers. So, it prints R*C*sizeof(int).
Question 3
#include <stdio.h>
int main()
{
    int a[5] = {1,2,3,4,5};
    int *ptr = (int*)(&a+1);
    printf("%d %d", *(a+1), *(ptr-1));
    return 0;
}
Tick
2 5
Cross
Garbage Value
Cross
Compiler Error
Cross
Segmentation Fault


Question 3-Explanation: 
The program prints “2 5″. Since compilers convert array operations in pointers before accessing the array elements, (a+1) points to 2. The expression (&a + 1) is actually an address just after end of array ( after address of 5 ) because &a contains address of an item of size 5*integer_size and when we do (&a + 1) the pointer is incremented by 5*integer_size. ptr is type-casted to int * so when we do ptr -1, we get address of 5
Question 4
#include <stdio.h>

char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;

int main()
{
    printf("%s ", **++cpp);
    printf("%s ", *--*++cpp+3);
    printf("%s ", *cpp[-2]+3);
    printf("%s ", cpp[-1][-1]+1);
    return 0;
}
Tick
TEST sQuiz Z CQ
Cross
MCQ Quiz Z CQ
Cross
TEST Quiz Z CQ
Cross
GarbageValue sQuiz Z CQ


Question 4-Explanation: 
Let us first consider **++cpp. Precedence of prefix increment and de-reference is same and associativity of both of them is right to left. So the expression is evaluated as **(++cpp). So cpp points to c+2. So we get \"TEST\" as output. Note the de-reference operator twice. Similarly, you may try other expressions yourself with the help of precedence table.
Question 5
Predict the output
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** str_ref)
{
    str_ref++;
}

int main()
{
    char *str = (void *)malloc(100*sizeof(char));
    strcpy(str, "GeeksQuiz");
    fun(&str);
    puts(str);
    free(str);
    return 0;
}
Tick
GeeksQuiz
Cross
eeksQuiz
Cross
Garbage Value
Cross
Compiler Error


Question 5-Explanation: 
Note that str_ref is a local variable to fun(). When we do str_ref++, it only changes the local variable str_ref. We can change str pointer using dereference operator *. For example, the following program prints \"eeksQuiz\"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** str_ref)
{
    (*str_ref)++;
}

int main()
{
    char *str = (void *)malloc(100*sizeof(char));
    strcpy(str, \"GeeksQuiz\");
    fun(&str);
    puts(str);
    free(str);
    return 0;
}
Question 6
Assume that the size of int is 4.
#include <stdio.h>
void f(char**);
int main()
{
    char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
    f(argv);
    return 0;
}
void f(char **p)
{
    char *t;
    t = (p += sizeof(int))[-1];
    printf("%s\n", t);
}
Cross
ab
Cross
cd
Cross
ef
Tick
gh


Question 6-Explanation: 
The expression (p += sizeof(int))[-1] can be written as (p += 4)[-1] which can be written as (p = p+4)[-] which returns address p+3 which is address of fourth element in argv[].
Question 7

C

Tick

2 3 5 6

Cross

2 3 4 5

Cross

4 5 0 0

Cross

none of the above



Question 7-Explanation: 

In the above code, we have declared:  int a[][3] = {1, 2, 3, 4, 5, 6};

This line declares a 2D array with 2 rows and 3 columns. The elements are initialized in row-major order: 1, 2, and 3 are the elements of the first row, and 4, 5, and 6 are the elements of the second row.

int (*ptr)[3] = a; This line declares a pointer to an array of 3 integers and assigns it the address of the first row of the array a. The pointer ptr now points to the first row of a.

printf("%d %d ", (*ptr)[1], (*ptr)[2]); his line prints the a[0][1]and a[0][2] elements of the first row of a, which are 2 and 3, respectively. So the output of this line is "2 3 ".

++ptr; This line increments the pointer ptr, causing it to point to the second row of a.

printf("%d %d\n", (*ptr)[1], (*ptr)[2]); This line prints the second and third elements of the second row of a, which are 5 and 6, respectively. The final output is "2 3 5 6".

Hence the correct answer is (A).

Question 8
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i;
    int *ptr = (int *) malloc(5 * sizeof(int));

    for (i=0; i<5; i++)
        *(ptr + i) = i;

    printf("%d ", *ptr++);
    printf("%d ", (*ptr)++);
    printf("%d ", *ptr);
    printf("%d ", *++ptr);
    printf("%d ", ++*ptr);
}
Cross
Compiler Error
Tick
0 1 2 2 3
Cross
0 1 2 3 4
Cross
1 2 3 4 5


Question 8-Explanation: 
The important things to remember for handling such questions are 1) Prefix ++ and * operators have same precedence and right to left associativity. 2) Postfix ++ has higher precedence than the above two mentioned operators and  associativity is from left to right. We can apply the above two rules to guess all *ptr++ is treated as *(ptr++) *++ptr is treated as *(++ptr) ++*ptr is treated as ++(*ptr)
Question 9
Output of following program
#include <stdio.h>
int fun(int arr[]) {
   arr = arr+1;	
   printf("%d ", arr[0]);
}
int main(void) {
   int arr[2] = {10, 20};
   fun(arr);
   printf("%d", arr[0]);
   return 0;
}
Cross
Compiler Error
Tick
20 10
Cross
20 20
Cross
10 10


Question 9-Explanation: 
In C, array parameters are treated as pointers (See http://www.geeksforgeeks.org/why-c-treats-array-parameters-as-pointers/ for details). So the variable arr represents an array in main(), but a pointer in fun().
Question 10
What is printed by the following C program?
$include <stdio.h>
int f(int x, int *py, int **ppz)
{
  int y, z;
  **ppz += 1; 
   z  = **ppz;
  *py += 2;
   y = *py;
   x += 3;
   return x + y + z;
}
 
void main()
{
   int c, *b, **a;
   c = 4;
   b = &c;
   a = &b; 
   printf( "%d", f(c,b,a));
   getchar();
}
Cross
18
Tick
19
Cross
21
Cross
22


Question 10-Explanation: 
There are 19 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads