Advanced Arrays and Pointers

0 comments Posted by Unknown at 15:51
Advanced C Arrays


In C, an array is formed by laying out all the elements contiguously in memory. The square bracket syntax can be used to refer to the elements in the array. The array as a whole is referred to by the address of the first element which is also known as the "base address" of the whole array.
{
int array[6];
int sum = 0;
sum += array[0] + array[1]; // refer to elements using []
}
0 1 2 3 4 5
array
Index
array[0] array[1] array[2] ...
The array name acts like a pointer to the first element- in this case an (int*). The programmer can refer to elements in the array with the simple [ ] syntax such as array. This scheme works by combining the base address of the whole array with the index to compute the base address of the desired element in the array. It just requires a little arithmetic. Each element takes up a fixed number of bytes which is known at compile-time. So the address of element n in the array using 0 based indexing will be at an offset of (n * element_size) bytes from the base address of the whole array. address of nth element = address_of_0th_element + (n * element_size_in_bytes)

Read More »

C variable and control Flow questions and Answers

0 comments Posted by Unknown at 14:43
C variable and control Flow questions and Answers
1. What is the difference between declaring a variable and defining a variable?
Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration.
Example: int a;
Here variable 'a' is declared of data type 'int' Defining a variable means declaring it and also allocating space to hold it. We can say "Definition = Declaration + Space reservation".
Example: int a = 10;
Here variable "a" is described as an int to the compiler and memory is allocated to hold value 10
.
Read More »

Solved C Apptitude

0 comments Posted by Unknown at 14:34

 C Apptitude

Predict the output or error(s) for the following:
15. #include
main()
{ char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
Answer:
77
Explanation:
p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 – 32), we get 77("M");
So we get the output 77 :: "M" (Ascii is 77).
Read More »

Infosys Solved Questions

0 comments Posted by Unknown at 14:27

Infosys Solved Questions

1.What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf (“%d",a[i]) ;
}
Ans: The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.
-------------------------------------------------------------------------------------------------
Read More »

Function question and Answers

0 comments Posted by Unknown at 14:15
Function question and Answers


1. When should I declare a function?
Functions that are used only in the current source file should be declared as static, and the function's declaration should appear in the current source file along with the definition of the function. Functions used outside of the current source file should have their declarations put in a header file, which can be included in whatever source file is going to use that function. For instance, if a function named stat_func() is used only in the source file stat.c, it should be declared as shown here:
/* stat.c */
#include <stdio.h>
static int stat_func(int, int); /* static declaration of stat_func() */
void main(void)
{
rc = stat_func(1, 2);
}
/* definition (body) of stat_func() */
static int stat_func(int arg1, int arg2)
{
return rc;
}
In this example, the function named stat_func() is never used outside of the source file stat.c. There is therefore no reason for the prototype (or declaration) of the function to be visible outside of the stat.c source file. Thus, to avoid any confusion with other functions that might have the same name, the declaration ofstat_func() should be put in the same source file as the declaration of stat_func().
Read More »

C Function questions and Answer

0 comments Posted by Unknown at 14:14

Interview Questions

1. What is a local block?
A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A C function contains left and right braces, and therefore anything between the two braces is contained in a local block. An if statement or a switch statement can also contain braces, so the portion of code between these two braces would be considered a local block.

Additionally, you might want to create your own local block without the aid of a C function or keyword construct. This is perfectly legal. Variables can be declared within local blocks, but they must be declared only at the beginning of a local block. Variables declared in this manner are visible only within the local block. Duplicate variable names declared within a local block take precedence over variables with the same name declared outside the local block. Here is an example of a program that uses local blocks:

Read More »

More on Pointer in c

0 comments Posted by Unknown at 19:21
1.What is meaning of following declaration?

int(*ptr[5])();

(A) ptr is pointer to function.

(B) ptr is array of pointer to function.

(C) ptr is pointer to such function which return type is array.

(D) ptr is pointer to array of function.

(E)None of these

Explanation:

Here ptr is array not pointer.



2.What is meaning of following pointer declaration?

int(*(*ptr1)())[2];

(A) ptr is pointer to function.

(B) ptr is array of pointer to function.

(C) ptr is pointer to such function which return type is pointer to an array.

(D) ptr is pointer array of function.

(E) None of these

Explanation: **

3.What is size of generic pointer in c?

(A) 0

(B) 1

(C) 2

(D) Null

(E) Undefined

Explanation:

Size of any type of pointer is 2 byte (In case of near pointer)

Note. By default all pointers are near pointer if default memory model is small.

4. What will be output of following c code?

#include<stdio.h>

int main(){

int *p1,**p2;

double *q1,**q2;

clrscr();

printf("%d %d ",sizeof(p1),sizeof(p2));

printf("%d %d",sizeof(q1),sizeof(q2));

getch();

return 0;

}

(A) 1 2 4 8

(B) 2 4 4 8

(C) 2 4 2 4

(D) 2 2 2 2

(E) 2 2 4 4

Explanation:

Size of any type of pointer is 2 byte (In case of near pointer)

5.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

char huge *p=(char *)0XC0563331;

char huge *q=(char *)0XC2551341;

if(p==q)

printf("Equal");

else if(p>q)

printf("Greater than");

else

printf("Less than");

return 0;

}

(A) Equal

(B) Greater than

(C) Less than

(D) Compiler error

(E) None of above

Explanation:

As we know huge pointers compare its physical address.

Physical address of huge pointer p

Huge address: 0XC0563331

Offset address: 0x3331

Segment address: 0XC056

Physical address= Segment address * 0X10 + Offset address

=0XC056 * 0X10 +0X3331

=0XC0560 + 0X3331

=0XC3891

Physical address of huge pointer q

Huge address: 0XC2551341

Offset address: 0x1341

Segment address: 0XC255

Physical address= Segment address * 0X10 + Offset address

=0XC255 * 0X10 +0X1341

=0XC2550 + 0X1341

=0XC3891

Since both huge pointers p and q are pointing same physical address soif condition will true.

6. What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

int a=5,b=10,c=15;

int *arr[]={&a,&b,&c};

printf("%d",*arr[1]);

return 0;

}

(A) 5

(B) 10

(C) 15

(D) Compiler error

(E) None of above

Explanation:

Array element cannot be address of auto variable. It can be address of static or extern variables.

7.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

int a[2][4]={3,6,9,12,15,18,21,24};

printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]);

return 0;

}

(A) 15 18 21

(B) 21 21 21

(C) 24 24 24

(D) Compiler error

(E) None of above

Explanation:

In c,

a [1][2]

=*(a [1] +2)

=*(*(a+1) +2)

=2[a [1]]

=2[1[a]]

Now, a [1] [2] means 1*(4) +2=6th element of an array staring from zero i.e. 21.

8. What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

const int x=25;

int * const p=&x;

*p=2*x;

printf("%d",x);

return 0;

}

(A) 25

(B) 50

(C) 0

(D) Compiler error

(E) None of above

Explanation:

const keyword in c doesn’t make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line:

int * const p=&x;

p is constant pointer while content of p i.e. *p is not constant.

*p=2*x put the value 50 at the memory location of variable x.

9.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

static char *s[3]={"math","phy","che"};

typedef char *( *ppp)[3];

static ppp p1=&s,p2=&s,p3=&s;

char * (*(*array[3]))[3]={&p1,&p2,&p3};

char * (*(*(*ptr)[3]))[3]=&array;

p2+=1;

p3+=2;

printf("%s",(***ptr[0])[2]);

return 0;

}

(A) math

(B) phy

(C) che

(D) Compiler error

(E) None of these

Explanation:

Here ptr is pointer to array of pointer to string. P1, p2, p3 are pointers to array of string. array[3] is array which contain pointer to array of string.

Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

As we know p[i]=*(p+i)

(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]

=(***(&array))[2] //ptr=&array

=(**array)[2] //From rule *&p=p

=(**(&p1))[2] //array=&p1

=(*p1)[2]

=(*&s)[2] //p1=&s

=s[2]=”che”

10.What will be output if you will compile and execute the following c code?

#include<conio.h>

#include<stdio.h>

int display();

int(*array[3])();

int(*(*ptr)[3])();

int main(){

array[0]=display;

array[1]=getch;

ptr=&array;

printf("%d",(**ptr)());

(*(*ptr+1))();

return 0;

}

int display(){

int x=5;

return x++;

}

(A) 5

(B) 6

(C) 0

(D) Compiler error

(E) None of these

Explanation:

In this example:

array []: It is array of pointer to such function which parameter is void and return type is int data type.

ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.

(**ptr)() = (** (&array)) () //ptr=&array

= (*array) () // from rule *&p=p

=array [0] () //from rule *(p+i)=p[i]

=display () //array[0]=display

(*(*ptr+1))() =(*(*&array+1))() //ptr=&array

=*(array+1) () // from rule *&p=p

=array [1] () //from rule *(p+i)=p[i]

=getch () //array[1]=getch

11.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

int i;

char far *ptr=(char *)0XB8000000;

*ptr='A';

*(ptr+1)=1;

*(ptr+2)='B';

*(ptr+3)=2;

*(ptr+4)='C';

*(ptr+5)=4;

return 0;

}

Output:

It output will be A, B and C in blue, green and red color respectively. As shown in following figure:

12.What will be output if you will compile and execute the following c code?

#include<stdio.h>

#include <dos.h>

int main(){

int j;

union REGS i,o;

char far *ptr=(char *)0XA0000000;

i.h.ah=0;

i.h.al=0x13;

int86(0x10,&i,&o);

for(j=1;j<=100;j++){

*(ptr+j)=4;

}

return 0;

}

Outpt:

One red color line in the graphics console as shown in the following figure:



13.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int dynamic(int,...);

int main(){

int x,y;

x=dynamic(2,4,6,8,10,12,14);

y=dynamic(3,6,9,12);

printf("%d %d ",x,y);

return 0;

}

int dynamic(int s,...){

void *ptr;

ptr=...;

(int *)ptr+=2;

s=*(int *)ptr;

return s;

}

(A) 8 12

(B) 14 12

(C) 2 3

(D) Compiler error

(E) None of these

Explanation:

In c three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to

first element of variable number of argument. After incrementing it will point third element.

14. Which of the following is not correct pointer declaration?

(i)int * const * ptr

(ii)int const * const * ptr;

(iii)const int ** const ptr;

(iv)const int const **ptr;

(v)int const ** const ptr;

(A) All are collect.

(B) Only (ii) is incorrect.

(C) Only (iv) is incorrect.

(D) Both (iii) and (v) are incorrect.

(E) All are incorrect.

Explanation: **

15. What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

char arr[]="C Question Bank";

float *fptr;

fptr=(float *)arr;

fptr++;

printf("%s",fptr);

return 0;

}

(A) C Question Bank

(B) Question Bank

(C) Bank

(D) estion Bank

(E)Compilation error

Explanation: **

16.In the following declaration ptr is

far * near * huge * ptr;

(A) Near pointer.

(B) Far pointer.

(C) Huge pointer.

(D) Near and far pointer.

(E) Near,far and huge pointer.

Explanation: **

17.What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

char arr[]="C Question Bank";

char *p;

p+=3;

p=arr;

p+=3;

*p=100;

printf("%s",arr);

return 0;

}

(A) C question Bank

(B) C quesdion Bank

(C) C qdestion Bank

(D) C q100estion Bank

(E) Compilation error

Explanation:

18. Which of the following ptr is not pointer?

(A) int(*ptr)()

(B) long **volatile*ptr

(C) int(*ptr[2])[3]

(D) float * (*ptr)[5]

(E) All are pointer

Explanation: **

19. Which of the following is incorrect c statement?

(A) We can increment array pointer

(B) We can increment function pointer

(C) We can increment structure pointer

(D) We can increment union pointer.

(E) We can increment generic pointer.

Explanation: **

20.Which of the following incorrect about far pointer?

(i)Size of far pointer is four byte.

(ii)Far pointer can points all segment of residence memory

(iii)If we will increment far pointer it can move from one segment to another segment.

Choose correct option:

(A) Only (i) is incorrect.

(B) Only (ii) is incorrect.

(C) Only (iii) is incorrect.

(D) Both (ii) and (iii) are incorrect.

(E) All three are incorrect.

Explanation: **





click the Below link download this file 
Read More »
 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top