C Pointer Basics
Question 1 |
# include <stdio.h>
void fun(int x)
{
x = 30;
}
int main()
{
int y = 20;
fun(y);
printf("%d", y);
return 0;
}
30 | |
20 | |
Compiler Error | |
Runtime Error |
Discuss it
Question 2 |
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
20 | |
30 | |
Compiler Error | |
Runtime Error |
Discuss it
Question 3 |
#include <stdio.h>
int main()
{
int *ptr;
int x;
ptr = &x;
*ptr = 0;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
*ptr += 5;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
return 0;
}
x = 0 *ptr = 0 x = 5 *ptr = 5 x = 6 *ptr = 6 | |
x = garbage value *ptr = 0 x = garbage value *ptr = 5 x = garbage value *ptr = 6 | |
x = 0 *ptr = 0 x = 5 *ptr = 5 x = garbage value *ptr = garbage value | |
x = 0 *ptr = 0 x = 0 *ptr = 0 x = 0 *ptr = 0 |
Discuss it
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 |
#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;
}
sizeof arri[] = 3
sizeof ptri = 4
sizeof arrc[] = 3
sizeof ptrc = 4 | |
sizeof arri[] = 12
sizeof ptri = 4
sizeof arrc[] = 3
sizeof ptrc = 1 | |
sizeof arri[] = 3
sizeof ptri = 4
sizeof arrc[] = 3
sizeof ptrc = 1 | |
sizeof arri[] = 12
sizeof ptri = 4
sizeof arrc[] = 3
sizeof ptrc = 4 |
Discuss it
Question 5 |
#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;
}
90.500000
3 | |
90.500000
12 | |
10.000000
12 | |
0.500000
3 |
Discuss it
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 byteNumber of elements between two pointer are: 5.
Number of bytes between two pointers are: 20 | |
Number of elements between two pointer are: 20.
Number of bytes between two pointers are: 20 | |
Number of elements between two pointer are: 5.
Number of bytes between two pointers are: 5 | |
Compiler Error | |
Runtime Error |
Discuss it
Question 7 |
#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;
}
What is the output of above program?Machine dependent | |
513 | |
258 | |
Compiler Error |
Discuss it
Question 8 |
C
int main()
{
char *ptr = "GeeksQuiz";
printf("%c", *&*&*ptr);
return 0;
}
Compiler Error | |
Garbage Value | |
Runtime Error | |
G |
Discuss it
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", *&*&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;
}
10 20 30 40 | |
Machine Dependent | |
10 20 | |
Northing |
Discuss it
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. #include
Question 10 |
The reason for using pointers in a C program is
Pointers allow different functions to share and modify their local variables. | |
To pass large structures so that complete copy of the structure can be avoided. | |
Pointers enable complex “linked" data structures like linked lists and binary trees. | |
All of the above |
Discuss it
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 pointer 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.

