Pointer - Study Mode

[#106] Determine Output: void main()
{
char far *farther, *farthest
printf("%d..%d", sizeof(farther), sizeof(farthest))
}
Correct Answer

(C) 4..4

Explanation

Solution: In the given C program code snippet: void main()
{
char far *farther, *farthest
printf("%d..%d", sizeof(farther), sizeof(farthest))
} The sizeof operator is used to determine the size, in bytes, of a variable or data type. 1. ** farther ** and ** farthest ** are both pointers of type **char***. In C, the size of a pointer is typically **4 bytes** on a 32-bit system or **8 bytes** on a 64-bit system. Assuming a standard 32-bit architecture, the size of both pointers would be **4 bytes**.
2. Therefore, the output of the printf function will be:
- sizeof(farther) = 4
- sizeof(farthest) = 4 The printf statement prints these two sizes, resulting in the output **"4..4"**. Hence, the correct answer is **Option C: 4..4**.

[#107] Determine Output: main()
{
char *str1 = "abcd"
char str2[] = "abcd"
printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd"))
}
Correct Answer

(C) 8 5 5

Explanation

Solution: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the 'null' termination character). The third sizeof is similar to the second one.

[#108] Comment on the following pointer declaration? int * ptr, p
Correct Answer

(A) ptr is a pointer to integer, p is not.

Explanation

Solution: int *ptr, p
ptr is declared as a pointer to an integer because of the `*` symbol next to it. This means that `ptr` can hold the address of an integer variable. p is declared as a regular integer variable, not a pointer. The absence of the `*` symbol means that `p` will hold an integer value directly, not an address. Key Points: - ptr : Pointer to integer, can store addresses of integer variables. - p : Regular integer variable, cannot store addresses.

[#109] What will be the output? main()
{
char *p
p = "Hello"
printf("%cn",*&*p)
}
Correct Answer

(B) H

Explanation

Solution: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string " Hello ". * p dereferences it and so its value is H . Again & references it to an address and * dereferences it to the value H .

[#110] Determine output: #include <stdio.h>
void main()
{
char *p = NULL
char *q = 0
if(p)
printf(" p ")
else
printf("nullp")
if(q)
printf("q")
else
printf(" nullq")
}
Correct Answer

(D) nullp nullq

Explanation

Solution: char * p = NULL is same as char * q = 0. In both declarations p and q are initialized to null.