C Pointers Test 1
1) In a structure, if a variable works as a pointer then from the given below operators which operator is used for accessing data of the structure using the variable pointer?
- %
- ->
- .
- #
The correct option is (b).
Explanation:
For a structure, Arrow ( ->) is used for access the data using pointer variable and Dot(.) operator can be used for accessing the data using normal structure variable.
2) For the array element a[i][j][k][2], determine the equivalent pointer expression.
- *(*(*(*(a+i)+j)+k)+2)
- *( ((a+m)+n+o+p)
- ((((a+m)+n)+o)+p)
- *( (((a+m)+n)+o+p)
The correct option is (a).
Explanation:
For the array element a[i][j] the pointer expression is *(*(a+i)+j)
For the array element a[i][j][k] the pointer expression is *(*(*(a+i)+j)+k)
For the array element a[i][j][k][2] the pointer expression is *(*(*(*(a+i)+j)+k)+2)
3) Are the expression ++*ptr and *ptr++ are same?
- True
- False
The correct option is (b).
Explanation:
++*ptr increments the value pointed by ptr and*ptr++ increments the pointer not the value.
4) Select the correct statement which is a combination of these two statements,
- char *p = (char*)malloc(100);
- char *p = (char) malloc(100);
- char p = *malloc(100);
- None of the above
The correct option is (a).
Explanation:
The below code is a prototype of malloc() function, here ptr indicates the pointer.
In below code, “*p” is a pointer of data type char and malloc() function is used for allocating the memory for char.
5) For the below mention C statement, what is your comment?
- Would throw Runtime error
- Improper typecasting
- Memory will be allocated but cannot hold an int value in the memory
- No problem with the statement
The correct option is (d).
Explanation:
The size of int and unsigned data type is same therefore there is no problem in a C statement:
signed int *p=(int*)malloc(sizeof(unsigned int));