Pointer Basics

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…


Question 1
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;
}
A
30
B
20
C
Compiler Error
D
Runtime Error
Pointer Basics    
Discuss it


Question 1 Explanation: 
Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using the function fun(). So how do we modify the value of a local variable of a function inside another function. Pointer is the solution to such problems. Using pointers, we can modify a local variable of a function inside another function. See the next question. Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.
Question 2
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;
}
A
20
B
30
C
Compiler Error
D
Runtime Error
Pointer Basics    
Discuss it


Question 2 Explanation: 
The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&y)’, address of y is passed so that y can be modified using its address.
Question 3
Output of following program?
#include <stdio.h>

int main()
{
    int *ptr;
    int x;

    ptr = &x;
    *ptr = 0;

    printf(" x = %d\n", x);
    printf(" *ptr = %d\n", *ptr);

    *ptr += 5;
    printf(" x  = %d\n", x);
    printf(" *ptr = %d\n", *ptr);

    (*ptr)++;
    printf(" x = %d\n", x);
    printf(" *ptr = %d\n", *ptr);

    return 0;
}
A
x = 0
*ptr = 0
x = 5
*ptr = 5
x = 6
*ptr = 6
B
x = garbage value
*ptr = 0
x = garbage value
*ptr = 5
x = garbage value
*ptr = 6
C
x = 0
*ptr = 0
x = 5
*ptr = 5
x = garbage value
*ptr = garbage value
D
x = 0
*ptr = 0
x = 0
*ptr = 0
x = 0
*ptr = 0
Pointer Basics    
Discuss it


Question 3 Explanation: 
See the comments below for explanation.
  int *ptr;  /* Note: the use of * here is not for dereferencing, 
               it is for data type int */
  int x;

  ptr = &x;   /* ptr now points to x (or ptr is equal to address of x) */
  *ptr = 0;   /* set value ate ptr to 0 or set x to zero */

  printf(" x = %d\n", x);   /* prints x =  0 */
  printf(" *ptr = %d\n", *ptr);  /* prints *ptr =  0 */


  *ptr += 5;        /* increment the value at ptr by 5 */
  printf(" x  = %d\n", x);  /* prints x = 5 */
  printf(" *ptr = %d\n", *ptr); /* prints *ptr =  5 */


  (*ptr)++;         /* increment the value at ptr by 1 */
  printf(" x  = %d\n", x);  /* prints x = 6 */
  printf(" *ptr = %d\n", *ptr);  /* prints *ptr =  6 */
Question 4
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;
}
A
sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
B
sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
C
sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
D
sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
Pointer Basics    
Discuss it


Question 4 Explanation: 
Size of an array is number of elements multiplied by the type of element, that is why we get sizeof arri as 12 and sizeof arrc as 3. Size of a pointer is fixed for a compiler. All pointer types take same number of bytes for a compiler. That is why we get 4 for both ptri and ptrc.
Question 5
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;
}
A
90.500000 3
B
90.500000 12
C
10.000000 12
D
0.500000 3
Pointer Basics    
Discuss it


Question 5 Explanation: 
When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.
Question 6
#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    printf("Number of elements between two pointer are: %d.", 
                                (ptr2 - ptr1));
    printf("Number of bytes between two pointers are: %d",  
                              (char*)ptr2 - (char*) ptr1);
    return 0;
}
Assume that an int variable takes 4 bytes and a char variable takes 1 byte
A
Number of elements between two pointer are: 5. Number of bytes between two pointers are: 20
B
Number of elements between two pointer are: 20. Number of bytes between two pointers are: 20
C
Number of elements between two pointer are: 5. Number of bytes between two pointers are: 5
D
Compiler Error
E
Runtime Error
Pointer Basics    
Discuss it


Question 6 Explanation: 
Array name gives the address of first element in array. So when we do '*ptr1 = arr;', ptr1 starts holding the address of element 10. 'arr + 5' gives the address of 6th element as arithmetic is done using pointers. So 'ptr2-ptr1' gives 5. When we do '(char *)ptr2', ptr2 is type-casted to char pointer and size of character is one byte, pointer arithmetic happens considering character pointers. So we get 5*sizeof(int)/sizeof(char) as a difference of two pointers.
Question 7
#include<stdio.h> 
int main() 
{ 
   int a; 
   char *x; 
   x = (char *) &a; 
   a = 512; 
   x[0] = 1; 
   x[1] = 2; 
   printf("%d\n",a);   
   return 0; 
}
What is the output of above program?
A
Machine dependent
B
513
C
258
D
Compiler Error
Pointer Basics    
Discuss it


Question 7 Explanation: 
Output is 513 in a little endian machine. To understand this output, let integers be stored using 16 bits. In a little endian machine, when we do x[0] = 1 and x[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine.
Question 8
int main()
{
 char *ptr = "GeeksQuiz";
 printf("%c\n", *&*&*ptr);
 return 0;
}
A
Compiler Error
B
Garbage Value
C
Runtime Error
D
G
Pointer Basics    
Discuss it


Question 8 Explanation: 
The operator * is used for dereferencing and the operator & is used to get the address. These operators cancel out effect of each other when used one after another. We can apply them alternatively any no. of times. In the above code, ptr is a pointer to first character of string g. *ptr gives us g, &*ptr gives address of g, *&*ptr again g, &*&*ptr address of g, and finally *&*&*ptr gives ‘g’ Now try below
int main()
{
 char *ptr = "GeeksQuiz";
 printf("%s\n", *&*&ptr);
 return 0;
}
Question 9
#include<stdio.h>
void fun(int arr[])
{
  int i;
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  for (i = 0; i < arr_size; i++)
      printf("%d ", arr[i]);
}

int main()
{
  int i;
  int arr[4] = {10, 20 ,30, 40};
  fun(arr);
  return 0;
} 
A
10 20 30 40
B
Machine Dependent
C
10 20
D
Northing
Pointer Basics    
Discuss it


Question 9 Explanation: 
In C, array parameters are always treated as pointers. So following two statements have the same meaning.
void fun(int arr[])
void fun(int *arr)
[] is used to make it clear that the function expects an array, it doesn’t change anything though. People use it only for readability so that the reader is clear about the intended parameter type. The bottom line is, sizeof should never be used for array parameters, a separate parameter for array size (or length) should be passed to fun(). So, in the given program, arr_size contains ration of pointer size and integer size, this ration= is compiler dependent. 1 #include void fun(int arr[], size_t arr_size) { int i; for (i = 0; i < arr_size; i++) printf("%d ", arr[i]); } int main() { int i; int arr[] = {10, 20 ,30, 40}; // Use of sizeof is fine here size_t n = sizeof(arr)/sizeof(arr[0]); fun(arr, n); return 0; } [/sourcecode] Output: 10 20 30 40
Question 10
The reason for using pointers in a Cprogram is
A
Pointers allow different functions to share and modify their local variables.
B
To pass large structures so that complete copy of the structure can be avoided.
C
Pointers enable complex “linked" data structures like linked lists and binary trees.
D
All of the above
Pointer Basics    
Discuss it


Question 10 Explanation: 
See below explanation (A) With pointers, address of variables can be passed different functions can use this address to access the variables. (B) When large structure variables passed or returned, they are copied as everything is passed and returned by value in C. This can be costly with structure containing large data. To avoid this copying of large variables, we generally use poitner for large structures so that only address is copied. (C) With pointers, we can implement “linked” data structures. Java uses reference variables to implement these data structures. Note that C doesn't support reference variables.
There are 32 questions to complete.

Company Wise Coding Practice    Topic Wise Coding Practice