Pointer Tutorial
C pointers interview questions and answers
Frequently asked technical objective types multiple choice pointer questions with explanation of placement in c programming language
Note: Linux GCC compilers and Visual C++ compiler doesn't support far and huge pointers.
1.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(A) 2 | ||
(B) 320 | ||
(C) 64 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 64
Turbo C ++4.5: 64
Linux GCC: 64
Visual C++: 64
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
So ptr is pointing only first 8 bit which color is green and Decimal value is 64.
What will be output of following program?
#include<stdio.h>
#include<conio.h>
int main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
return 0;
return 0;
}
(A) NULL | ||
(B) cquestionbank.blogspot.com | ||
(C) c | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: cquestionbank.blogspot.com
Turbo C ++4.5: cquestionbank.blogspot.com
Linux GCC: Compilation error
Visual C++: Compilation error
p
is designated to point to a function with a void parameter and a void return type. In contrast, both r
and q
are configured as pointers to functions that take void parameters and return integers. Consequently, these pointers are appropriately designed to hold the addresses of functions with the specified parameter and return types.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
return 0;
}
(A) Address, Address, 3 | ||
(B) Address, 3, 3 | ||
(C) 3, 3, 3 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: Address, Address, 3
Turbo C ++4.5: Address, Address, Address
Linux GCC: Address, Address, 3
Visual C++: Address, Address, 3
Memory representation
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.
Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a = a
So *k = *(&j) since k = &j
*&j = j = 6024
And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3
What will be output of following program?
#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 80;
(*p)++;
printf("%d",*q);
}
(A) 80 | ||
(B) 81 | ||
(C) 82 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 81
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Far address of p and q are representing same physical address.
Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555
Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555
*p = 80, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 81
*q also means content at memory location 0x55555 which is 26
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions | ||
(B) c (null) | ||
(C) (null) (null) | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: (null) (null)
Turbo C ++4.5: Run time error
Linux GCC: Run time error
Visual C++: Run time error
strcpy
function.
What will be output of following program?
#include<stdio.h>
int main(){
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
return 0;
}
(A) power of pointer | ||
(B) power of c | ||
(C) power of cpower of c | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: power of pointer
Turbo C ++4.5: power of c
Linux GCC: Compilation error
Visual C++: Compilation error
a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995
b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995
Here both huge addresses are representing same physical address. So a==b is true.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}(A) 25 | ||
(B) 4 | ||
(C) Address | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
&a
, is not applicable.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
return 0;
}
(A) 2 2 | ||
(B) 4 4 | ||
(C) 4 2 | ||
(D) 2 4 | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 4 4
Turbo C ++4.5: 4 4
Linux GCC: Compilation error
Visual C++: Compilation error
p
is specifically defined as a far pointer with a size of 4 bytes. In contrast, by default, pointer q
is considered a near pointer with a size of 2 bytes.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10 | ||
(B) Address | ||
(C) 2 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
//if a=25
(A) 25 | ||
(B) Address | ||
(C) 0 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
&a
, is not permissible.
What will be output of following program?
#include<stdio.h>
int main(){
char arr[10];
arr = "world";
printf("%s",arr);
return 0;
}
(A) world | ||
(B) w | ||
(C) Null | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
A compilation error with the message "Lvalue required" occurs when attempting to assign a value to an array name. The reason is that an array name is essentially a constant pointer, and in C, it's not permissible to assign any value to a constant data type after its declaration.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)0;
double *s = 0;
a = (int)(p+1);
b = (int)(q+1);
c = (int)(r+1);
d = (int)(s+1);
printf("%d %d %d %d",a,b,c,d);
return 0;
return 0;
}
(A) 2 2 2 2 | ||
(B) 1 2 4 8 | ||
(C) 1 2 2 4 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 1 2 4 8
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Given that the initial address of all data types is zero, it follows that the subsequent address will increment by the size of the respective data type.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1 | ||
(B) 5 | ||
(C) -5 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 1
Turbo C ++4.5: 1
Linux GCC: 1
Visual C++: 2
What will be output of following program?
#include<stdio.h>
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
return 0;
}
(A) 1 | ||
(B) 2 | ||
(C) 3 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 3
Turbo C ++4.5: 3
Linux GCC: 3
Visual C++: 3
What will be output of following program?
#include<stdio.h>
int main(){
int * p , b;
b = sizeof(p);
printf("%d" , b);
return 0;
return 0;
}
(A) 2 | ||
(B) 4 | ||
(C) 8 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 2 or 4
Turbo C ++4.5: 2 or 4
Linux GCC: 4
Visual C++: 4
As the question does not specify the type of pointer
p
, the output will depend on the chosen memory model. By default, the memory model is set to small.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}(A) 5 5 | ||
(B) Address Address | ||
(C) 5 Address | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 5 5
Turbo C ++4.5: 5 5
Linux GCC: 5 5
Visual C++: 5 5
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address | ||
(B) Address Address | ||
(C) Address 5 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: Address Address
Turbo C ++4.5: Address Address
Linux GCC: Address Address
Visual C++: Address Address
i.e. *&a = a
so *&p = p which store address of integer i
&*p = &*(&i) //since p = &i
= &(*&i)
= &i
So second output is also address of i
What will be output of following program?
#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}(A) value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
| ||
(B) value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address
| ||
(C) value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address
| ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Turbo C ++4.5:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Linux GCC:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Visual C++:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Within the scope of any variable, it's possible for the value of the variable to change, but the address of the variable remains constant through any modifications.
What will be output of following program?
#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
return 0;
}(A) 25 | ||
(B) Address | ||
(C) Garbage | ||
(D) Compilation error | ||
(E)None of above |
Explanation:
Turbo C++ 3.0: 26
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
0x55550005 = 0x5555 * ox10 + ox0005 = 0x55555
Physical address of
0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555
*p = 25, means content at memory location 0x55555 is assigning value 25
(*p)++ means to increase the content by one at memory the location 0x5555 so now content of memory location at 0x55555 is 26
*q also means content at memory location 0x55555 which is 26
What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
return 0;
}
(A) 3 Address 3 | ||
(B) 3 Address Address | ||
(C) 3 3 3 | ||
(D) Compilation error | ||
(E) None of above |
Explanation:
Turbo C++ 3.0: 3 Address Address
Turbo C ++4.5: 3 Address Address
Linux GCC: 3 Address Address
Visual C++: 3 Address Address
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Pointer Tutorial
24 comments:
according to Q14,there is no explanation please provide explanation,it is very good for beginners...otherwise everthing fine...
ITS VERY GOOD JOB..PLEASE KEEP IT UP
will this program face dangling pointer problem???
#include
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
return 0;
}
i want a C program that takes the input – the mark sheets of N number of students.
for examle;
●Enter the marks for student 1 student name: aaa
roll number: 24
marks obtained in 5 subjects: 56 89 87 76 98
●Enter the roll number of the student to be searched: 22
●The details of student with roll number 22:
name: bbb
total marks obtained: 293
i need a C program that accepts the roll numbers and names of N students from the user … and prints those in the alphabetical order of the name, along with the roll numbers.
Enter the number of students: 3
Enter the name of student #1: vinay
Enter vinay’s roll number: 12
Enter the name of student #2: anand
Enter anand’s roll number: 14
Enter the name of student #3: kumar
Enter kumar’s roll number: 11
The list of students, in sorted order of names is given below:
anand (14)
kumar (11)
vinay (12)
what is O/P of this n please also tell whats the concept behind that o/p?
#include
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
should give an error as q doesn't point to a location.
Plz help:
Initialize an integer array, get values of array from user and store them in the array using pointer.
In Q-3, why output of **k is address in turbo c++ 4.5?give the explanataion.
In q-6, why output is power of c in turbo c++4.5? Plz explain that.
use structure concept
explain me q-2...please
explain me q-4
Use structures using arrays with sorting proram
Question 1: 320 in binary is 101000000. But it is different in the explanation. Is that the little endian representation of 320?
program to convert float to binary
program to convert float to binary
very good post.
very good post.
no it wont because of keyword static which makes the array to be stored in data area instead of stack (which is volatile).
output: 0----10
this is because,in array "a" the last value is a[1][1][1] so while printing value at "a[2][2][2]" it prints null value since array does check boundary. *p=0
*q= ***a => **(a+0) => **(a[0]) => *(a[0]+0)=>*(a[0][0])=>a[0][0][0]
so the *q = 10
float a,*b();here *b() meaning ?
will give garbage or error for p,as a[2][2][2]=*(*(*(a+2)+2)+2) but that array element does not exist as array always starts from zero.
Aivivu chuyên vé máy bay, tham khảo
vé máy bay đi Mỹ hạng thương gia
bay từ california về việt nam mất bao lâu
vé máy bay giá rẻ nhật việt
vé máy bay từ đức về sài gòn
thông tin chuyến bay từ canada về việt nam
Các chuyến bay từ Incheon về Hà Nội hôm nay
khách sạn cách ly ở vân đồn
Nice and very helpful C Programming articles... keep posting...
C pattern programming examples
C Arrays examples
Loops in C examples
Post a Comment