Pointers store address of variables or a memory location.

// General syntax
datatype *var_name; 

// An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
int *ptr;  

Read More..

 

Advanced Pointer

Question 1
void fun(int *p) 
{ 
  int q = 10; 
  p = &q; 
}     
  
int main() 
{ 
  int r = 20; 
  int *p = &r; 
  fun(p); 
  printf("%d", *p); 
  return 0; 
}
A
10
B
20
C
Compiler error
D
Runtime Error
Advanced Pointer    
Discuss it


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 adderess 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;
}
A
200
B
4
C
800
D
80
Advanced Pointer    
Discuss it


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;
}
A
2 5
B
Garbage Value
C
Compiler Error
D
Segmentation Fault
Advanced Pointer    
Discuss it


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;
}
A
TEST sQuiz Z CQ
B
MCQ Quiz Z CQ
C
TEST Quiz Z CQ
D
GarbageValue sQuiz Z CQ
Advanced Pointer    
Discuss it


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;
}
A
GeeksQuiz
B
eeksQuiz
C
Garbage Value
D
Compiler Error
Advanced Pointer    
Discuss it


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);
}
A
ab
B
cd
C
ef
D
gh
Advanced Pointer    
Discuss it


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
#include <stdio.h>
int main()
{
    int a[][3] = {1, 2, 3, 4, 5, 6};
    int (*ptr)[3] = a;
    printf("%d %d ", (*ptr)[1], (*ptr)[2]);
    ++ptr;
    printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
    return 0;
}
 
A
2 3 5 6
B
2 3 4 5
C
4 5 0 0
D
none of the above
Advanced Pointer    
Discuss it


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);
}
A
Compiler Error
B
0 1 2 2 3
C
0 1 2 3 4
D
1 2 3 4 5
Advanced Pointer    
Discuss it


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;
}
A
Compiler Error
B
20 10
C
20 20
D
10 10
Advanced Pointer    
Discuss it


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();
}
A
18
B
19
C
21
D
22
Advanced Pointer    GATE CS 2008    
Discuss it


Question 10 Explanation: 
Question 11
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.
#include <stdio.h>
int main()
{ 
   unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, 
                           {7, 8, 9}, {10, 11, 12}};
   printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}
A
2036, 2036, 2036
B
2012, 4, 2204
C
2036, 10, 10
D
2012, 4, 6
Advanced Pointer    GATE-CS-2015 (Set 1)    
Discuss it


Question 11 Explanation: 
x = 2000

Since x is considered as a pointer to an 
array of 3 integers and an integer takes 4
bytes, value of x + 3 = 2000 + 3*3*4 = 2036

The expression, *(x + 3) also prints same 
address as x is 2D array.


The expression *(x + 2) + 3 = 2000 + 2*3*4 + 3*4
                            = 2036
Question 12
Consider the following C program.
# include 
int main( )
{
  static int a[] = {10, 20, 30, 40, 50};
  static int *p[] = {a, a+3, a+4, a+1, a+2};
  int **ptr = p;
  ptr++;
  printf("%d%d", ptr - p, **ptr};
}
The output of the program is _________
A
140
B
120
C
100
D
40
Advanced Pointer    GATE-CS-2015 (Set 3)    
Discuss it


Question 12 Explanation: 
  In order to simplify programs involving complex operations on pointers, we suggest you to draw proper diagrams in order to avoid silly mistakes. Let’s assume that integer is of 4 Bytes and Pointer size is also 4 Bytes. Let’s assume array a Base address is 1000. Array name actually holds the array base address. pranjul_36 Let’s assume array p Base address is 2000. pranjul_36_1   Double Pointer ptr Base Address is 3000. pranjul_36_2   Now ptr is actually pointing to the first element of array p. ptr++ will make it point to the next element of array p. Its value will then change to 2004. One of the Rule of Pointer Arithmetic is that When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them. ptr is pointing to the second element and  p is pointing to the first element so ptr-p will be equal to 1(Excluding the element to which ptr is pointing). Now ptr = 2004 -----> *(2004) = 1012 ----> *(1012) ----> 40 which is the final answer.   This solution is contributed by Pranjul Ahuja. .  
Question 13
#include "stdlib.h"
int main()
{
 int *pInt;
 int **ppInt1;
 int **ppInt2;

 pInt = (int*)malloc(sizeof(int));
 ppInt1 = (int**)malloc(10*sizeof(int*));
 ppInt2 = (int**)malloc(10*sizeof(int*));

 free(pInt);
 free(ppInt1);
 free(*ppInt2);
 return 0;
}
Choose the correct statement w.r.t. above C program.
A
malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
B
free(*ppInt2) is not correct. It’ll give compile time error.
C
free(*ppInt2) is not correct. It’ll give run time error.
D
No issue with any of the malloc() and free() i.e. no compile/run time error.
Advanced Pointer    C Quiz - 101    
Discuss it


Question 13 Explanation: 
ppInt2 is pointer to pointer to int. *ppInt2 is pointer to int. So long as the argument of free() is pointer, there's no issue. That's why B) and C) both are not correct. Allocation of both ppInt1 and ppInt2 is fine as per their data type. So A) is also not correct. The correct statement is D).
Question 14
#include "stdio.h" 
int main()
{
 void *pVoid;
 pVoid = (void*)0;
 printf("%lu",sizeof(pVoid));
 return 0;
}
Pick the best statement for the above C program snippet.
A
Assigning (void *)0 to pVoid isn’t correct because memory hasn’t been allocated. That’s why no compile error but it'll result in run time error.
B
Assigning (void *)0 to pVoid isn’t correct because a hard coded value (here zero i.e. 0) can’t assigned to any pointer. That’s why it'll result in compile error.
C
No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.
D
sizeof() operator isn’t defined for a pointer of void type.
Advanced Pointer    C Quiz - 101    
Discuss it


Question 14 Explanation: 
(void *)0 is basically NULL pointer which is used for many purposes in C. Please note that no matter what is the type of pointer, each pointer holds some address and the size of every pointer is equal to sizeof(int). So D) isn't correct. An absolute address can be assigned to any pointer though it might result in issues at run time if the address is illegal. Since 0 is a legal address, assigning (void *)0 to pVoid is fine. So B) isn't correct. We aren't doing any illegal operation with pVoid here. So it'll not result in any compile/run time error. So A) isn't correct. For example, if we perform illegal operation over pVoid e.g. de-referencing of void pointer i.e. *pVoid, it'll result in error. The above program will compile/run without any issue. So C) is correct.
Question 15
In the context of the below program snippet, pick the best answer.
#include "stdio.h"
int arr[10][10][10];
int main()
{
 arr[5][5][5] = 123;
 return 0;
}
Which of the given printf statement(s) would be able to print arr[5][5][5]
(i) printf("%d",arr[5][5][5]);
(ii) printf("%d",*(*(*(arr+5)+5)+5));
(iii) printf("%d",(*(*(arr+5)+5))[5]);
(iv) printf("%d",*((*(arr+5))[5]+5));
A
only (i) would compile and print 123.
B
both (i) and (ii) would compile and both would print 123.
C
only (i), (ii) and (iii) would compile but only (i) and (ii) would print 123.
D
only (i), (ii) and (iii) would compile and all three would print 123.
E
all (i), (ii), (iii) and (iv) would compile but only (i) and (ii) would print 123.
F
all (i), (ii), (iii) and (iv) would compile and all would print 123.
Advanced Pointer    C Quiz - 102    
Discuss it


Question 15 Explanation: 
For arrays, we can convert array subscript operator [] to pointer deference operator * with proper offset. It means that arr[x] is equal to *(arr+x). Basically, these two are interchangeable. The same concept can be applied in multi-dimensional arrays as well. That's why all of the above 4 printf are referring to the same element i.e. arr[5][5][5]
Question 16
Find out the correct statement for the following program.
#include "stdio.h"

typedef int (*funPtr)(int);

int inc(int a)
{
 printf("Inside inc() %d\n",a);
 return (a+1);
}

int main()
{

 funPtr incPtr1 = NULL, incPtr2 = NULL;

 incPtr1 = &inc; /* (1) */
 incPtr2 = inc; /* (2) */

 (*incPtr1)(5); /* (3) */
 incPtr2(5); /* (4)*/

 return 0;
}
A
Line with comment (1) will give compile error.
B
Line with comment (2) will give compile error.
C
Lines with (1) & (3) will give compile error.
D
Lines with (2) & (4) will give compile error.
E
No compile error and program will run without any issue.
Advanced Pointer    C Quiz - 109    
Discuss it


Question 16 Explanation: 
While assigning any function to function pointer, & is optional. Same way, while calling a function via function pointer, * is optional.
There are 16 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.