C-Pointers Question and Answer
C-Pointers Question and Answer
81. char ptrl [ ] = "Drama Actor ";
char *ptr2 = malloc ( 5 );
ptr2 = ptrl;
What is wrong with the above code (assuming the call to malloc does not fail )?
char *ptr2 = malloc ( 5 );
ptr2 = ptrl;
What is wrong with the above code (assuming the call to malloc does not fail )?
- There will bea memory overwrite.
- It will not compile.
- There will be a segmentation fault.
- Not enough space is allocated by the malloc.
82. char echo [ 50 ] = "Brain Power";
char *ptr = echo + 5;
From the sample above, which would be the proper way to copy 20 bytes from the location pointted to by ptr to the beginning of echo ?
char *ptr = echo + 5;
From the sample above, which would be the proper way to copy 20 bytes from the location pointted to by ptr to the beginning of echo ?
- memcpy (echo, ptr, 20);
- It cannot be done, because the source and destination overlap.
- strncpy (echo, ptr, 20);
- memmove (echo, ptr, 20);
83. char sub1 [ 100] = "tamil";
char sub2 [100] = "Hindi";
char *strptrl = sub1 + 2;
char *strptr2=sub2 + 3;
strcpy (strptrl, sub2 );
strcpy ( strptr2, sub1 );
printf ("%s\n", sub1 );
printf ( "%s\n", sub2 );
Given the sample code above, which of the following string values will be printed when the code is executed ?
char sub2 [100] = "Hindi";
char *strptrl = sub1 + 2;
char *strptr2=sub2 + 3;
strcpy (strptrl, sub2 );
strcpy ( strptr2, sub1 );
printf ("%s\n", sub1 );
printf ( "%s\n", sub2 );
Given the sample code above, which of the following string values will be printed when the code is executed ?
- Ta Hin Tamil Hin Tamil
- Tam Hindi HiTamHindi
- TaHindi HinTahindi
- TaHindi HintTamil
84. Scrutinize the following code in C.
char fruit [ ] = "Orange";
char *ptr:
ptr = fruit;
What will be the output for the following expression ?
printf ("%c", * (ptr + 2 ));
char fruit [ ] = "Orange";
char *ptr:
ptr = fruit;
What will be the output for the following expression ?
printf ("%c", * (ptr + 2 ));
- r
- a
- n
- none
85. In the given code:
int a[50];
int *ptr;
ptr = a;
To access the 7th element of the array which of the following is incorrect ?
int a[50];
int *ptr;
ptr = a;
To access the 7th element of the array which of the following is incorrect ?
- *(a+6)
- a[6]
- ptr[6]
- *(*ptr+6)
86. main ( )
{
int a [5] = {-2, -1, 3, 4, 5};
int *b;
b = &a [ 2];
Then the value of b[-1] is;
{
int a [5] = {-2, -1, 3, 4, 5};
int *b;
b = &a [ 2];
Then the value of b[-1] is;
- 4
- 3
- -1
- -2
87. main ()
{
int a = 5, *ptr; ptr = &a;
printf ("%d", ++*ptr);
}
The output might be:
{
int a = 5, *ptr; ptr = &a;
printf ("%d", ++*ptr);
}
The output might be:
- 6
- 5
- 0
- none
88. int x, array [10];
From the sample above, which o the following is not a valid initialization for ptr?
From the sample above, which o the following is not a valid initialization for ptr?
- int *ptr = *array;
- int *ptr = 9 + array;
- int *ptr = &x;
- int *ptr = ptr = (int *) 0xa1000;
89. int Y[4] = {5, 6, 7, 8};
Which of the following will declare a pointer variable that points to the array in the sample code above ?
Which of the following will declare a pointer variable that points to the array in the sample code above ?
- int *(yptr[4]) = &y;
- int (yptr *) [4] = &y;
- int (*yptr) [4] = &y;
- int *yptr [4] = &y;
90. int x [ ] = {1, 2, 3, 4, 5};
inr *pre, **ptr2;
ptr = x;
ptr2 = &ptr;
Refering to the sample code above, how would you update x[2] to 10 using ptr2?
inr *pre, **ptr2;
ptr = x;
ptr2 = &ptr;
Refering to the sample code above, how would you update x[2] to 10 using ptr2?
- **(ptr2 + 2) = 10;
- * (&ptr2 + 2) = 10;
- *(*ptr2 + 2) = 10;
- (**ptr2 + 2) = 10;