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

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as