Skip to main content

Puzzle code of pointer



Q1) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a=10,*p,**q,***r;
p=&a;
q=&p;
r=&q;
printf("%d",*(*(*r))+5);
getch();
}

Ans:-15

Q2) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a[][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
printf("%d", *(*(a+1)+1));
getch();
}
Ans:-5

Discussion:-

Q3) Output of the program

#include "stdio.h"
#include "conio.h"
void print()
{
printf("sourav");
}

main()
{
void (*p)(); //Pointer of function declaration.
p=print; //Function address assignment to pointer.
p(); //Function call.
getch();
}
Ans:-sourav

Discussion:-Here p is a pointer of function print(); And the print() is called by p().

Q4)Output of the program.

#include "stdio.h"
#include "conio.h"
main()
{
int a[2][2][2]={1,2,3,4,5,6,7,8};
printf("%d ", *(*(*(a+1)+1)+1));
printf("%d ",***a);
getch();
}
Ans:-8 1

Discussion:-here a is a three dimension array. The first printf statement will print 8,the expression is equivalent to a[2][2][2] and second printf statement will print the a[0][0][0] th value of array a.

Q5)Is the code Wright.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
main()
{
void *p;
p=malloc(100);
getch();
}
Ans:-No

Discussion:-To correct to coding it is need to cast the pointer. The correct code is p=(char*)malloc(100);

Q6) Output of the program.

#include "stdio.h"
#include "conio.h"
struct abc
{
int a,b;
};
struct abc t,*p;
main()
{
p=&t;
p->a=10;
p->b=20;
printf("%d %d",p->a,p->b);
getch();
}
Ans:-10 20

Discussion:-Here p is a pointer of structure. p takes the address of a structure variable .and the arrow operator(->) is used to access the member of the structure.

Q7)Output of the program.

#include "stdio.h"
#include "conio.h"
main()
{
char *p="%d";
printf(p,500);
getch();
}

Ans:-500

Discussion:
-Here p is a character pointer. And hold the base address of the string “%d”.
In printf function p pointer replace by the string “%d”.

Q8)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a[]={10,20};
int *p=a;
*p++;
printf("%d ",*p);
++*p;
printf("%d ",*p);
getch();
}

Ans:-20 21

Discussion:-The statement *p++ increment the content of p pointer, so it will point the second element of array a. And the statement ++*p will increment the content of p pointer.

Q9)Output of the program.


#include "stdio.h"
#include "conio.h"
#include "string.h"
main()
{
char *p="sourav";
char *q="kayal";
char a[20];
int k=0;
while(*p!=NULL)
{
a[k]=*p;
p++;
k++;
}
while(*q!=NULL)
{
a[k]=*q;
q++;
k++;
}
puts(a);
getch();
}

Ans:-souravkayal


Discussion:-This is the program of string concatenation using pointer. Here scale factor of character pointer is 1.

Q10) output of the program.


#include "stdio.h"
#include "conio.h"
main()
{
printf("%d %d",sizeof(NULL),sizeof(""));
getch();
}

Ans:-It depends upon compiler.
In xp TC++ environment it prints 2 1
In window7 , DEV C++ environment it prints 4 1

Q11)Output of the program


#include "stdio.h"
#include "conio.h"
main()
{
int *p;
char *q;
float *r;
printf("%d %d %d",sizeof(p),sizeof(q),sizeof(r));
getch();
}

Ans:-Output is compiler dependent
Xp and TC combination gives 2 2 2
Window7 and DEV C++ gives 4 4 4.
Discussion:-It depends upon system memory byte size.

Q12)Find out the error.


#include "stdio.h"
#include "conio.h"
main()
{
int *p;
*p=100;
printf("%d",*p);
getch();
}
Ans:-The program is syntactically correct. But no output will come.

Discussion:-Here p is a integer pointer but not initialize by and address. So in run time it will take random address.



Q13)Output of program.

#include "stdio.h"
#include "conio.h"
main()
{

struct abc
{
int a;
int b;
};
struct abc t,*p;
t.a=100;
t.b=200;
printf("%d",sizeof(p));
getch();
}

Ans:-Output depends upon platform.
In XP and TC++ it will give 2
In Window 7 DEV C++ it will give 4

Comments

Popular posts from this blog

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)