C pointer questions

C pointer questions

1. What is the output of following program?

# include < stdio.h >
void
fun(int a)
{
a = 30;
}
int main()
{
int y = 20;
fun(y);
printf(“%d”, y);
return 0;
}

a. 30
b. 20
c. runtime error
d. compiler error

Show Answer

Answer: (B) 20
Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using the function fun(). So how do we modify the value of a local variable of a function inside another function. Pointer is the solution to such problems. Using pointers, we can modify a local variable of a function inside another function.

2.Output of following program ?

# include
void
fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int x= 20;
fun(&x);
printf(“%d”, x);
return 0;
}

a. 20
b. 30
c compiler Error
d. runtime error

Show Answer

answer(b) The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&x)’, address of x is passed so that x can be modified using its address.

3. Can you combine the following two statements into one?

a. char p = *malloc(100);
b. char *p = (char) malloc(100);
c. char *p = (char *)(malloc*)(100);
d. char *p = (char*)malloc(100);

Show Answer

Answer (d)
Explanation: no

4. In which header file is the NULL macro defined?

a. stdio.h
b. stddef.h
c. math.h
d. stdio.h and stddef.h

Show Answer

Answer: Option (d)
Explanation:
The macro “NULL” is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

5. Assume the float takes 4 bytes, predict the output of following program?

#include
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;
}

a. 0.5000003
b. 90.5000011
c. 10.0000013
d. 90.5000003

Show Answer

Answer: (d)
Explanation: When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.

6. What is the output of this C code?

#include
int
main()
{
int a;
char *y;
y = (char *) &,a;
a = 512;
y[0] = 1;
y[1] = 2;
printf(“%dn”,a);
return 0;
}

a.Machine dependent
b.513
c.258
d.Compiler Error

Show Answer

Answer: (a)
Explanation: Output is 513 in a little endian machine. To understand this output, let integers be stored using 16 bits. In a little endian machine, when we do y[0] = 1 and y[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine.

7. What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf(“%d\n”, (int)*p);
return 0;
}

a. Compile time error
b. B. Segmentation fault/runtime crash
c. 10
d. Undefined behaviour

Show Answer

Answer: Option (a)
Explanation:
None.

8. What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf(“%f\n”, *(float*)p);
return 0;
}

a. Compile time error
b. Undefined behaviour
c. 10
d. 0.000000

Show Answer

Answer: Option (a)
Explanation:
None.