Consider the following two statements: char S[20]="aardvark"; char *p = "baboon"; p = S+2; p[3] = 0;The best description of the situation after these four statements is that:Group of answer choicesThe code can be compiled, but when it is executed it will give a run-time error because pointer p points to restricted memory used to store string constantsThe code will not compile correctly because of type mismatches, and hence cannot be executedPointer p points at the string "rdv"Pointer p points at the string "rdv0rk"Pointer p points at the string "baboo0"
Question
Consider the following two statements: char S[20]="aardvark"; char *p = "baboon"; p = S+2; p[3] = 0;The best description of the situation after these four statements is that:Group of answer choicesThe code can be compiled, but when it is executed it will give a run-time error because pointer p points to restricted memory used to store string constantsThe code will not compile correctly because of type mismatches, and hence cannot be executedPointer p points at the string "rdv"Pointer p points at the string "rdv0rk"Pointer p points at the string "baboo0"
Solution
The best description of the situation after these four statements is that:
"Pointer p points at the string "rdv""
Explanation:
In the first statement, we declare a character array S and initialize it with the string "aardvark".
In the second statement, we declare a pointer to a character and initialize it to point to the string "baboon".
In the third statement, we change p to point to the third character of the string in S (remember that array indexing starts at 0, so S+2 points to the third character, 'r').
In the fourth statement, we change the fourth character that p points to (which is 'd') to the null character (0).
So, after these four statements, p points to the string "rdv", which is a substring of "aardvark" starting from the third character and ending at the character before the null character we just inserted.
Similar Questions
Consider these two statements: char v[6]; strcpy(v, "string");The best description of the situation after these two statements is that:Group of answer choicesThe call to strcpy() will corrupt memory and may result in a program run-time error at some later point in the program's executionA run-time array overflow will be detected, and the program will be halted and an error message printedThe call to strcpy() will be truncated, and only "tring" will be copied into vThe call to strcpy() is completely fine, and v will have the desired value of "string"The call to strcpy() will be truncated, and only "strin" will be copied into v
Q4: Consider these three statements: char S[10]="trouble"; char *t=S+2; *(t+3) = '\0';The best description of the situation after these three statements is that:Array S now contains the string "troub"Array S now contains the string "tro"Array S now contains the string "tro\0uble"Array S still contains the string "trouble"The code will not compile correctly, and hence cannot be executed
In the following code, which of the variables, a, b, c, d and e are strings/pointer-to-strings when code is at /* A */?#include <string.h>#include <stdio.h>void f(char *p) { p = "hello";}int main() { char a[16], c[16], *d, e[16]; char b[16] = {'h', 'e', 'l', 'l', 'o'}; strcpy(a, "hello"); f(c); d = "hello"; scanf("%s", e); // assume that the end-user entered hello\n /* A */ return 0;}
#ifndef main.h#define main.h#include <stddef.h>int _putchar(char c);int _islower(int c);int _isalpha(int c);int _abs(int n);int _isupper(int c);int _isdigit(int c);int _strlen(char *s);void _puts(char *s);char *_strcpy(char *dest, char *src);int _atoi(char *s);char *_strcat(char *dest, char *src);char *_strncat(char *dest, char *src, int n);char *_strncpy(char *dest, char *src, int n);int _strcmp(char *s1, char *s2);char *_memset(char *s, char b, unsigned int n);char *_memcpy(char *dest, char *src, unsigned int n);char *_strchr(char *s, char c);unsigned int _strspn(char *s, char *accept);char *_strpbrk(char *s, char *accept);char *_strstr(char *haystack, char *needle);#endif main.h~ ~ ~
Consider the following declarations:char a[] = "abhijit";char b[16] = {'a', 'b', 'h', 'i', 'j', 'i', 't'};char *c = "abhijit";char d[16] = "abhijit";Mark the statements as True/False w.r.t. the above.Read each word and number carefully.True False both b and d contain some random data elements of array a[] can not be modified contents of arrays b and c can b modified a points to a constant array, and sayinga[2]= 'x'will cause sgfault c points to a constant string, and sayingc[2] = 'x'will cause a segfault. sizeof(a) and sizeof(c) are 8 bytes b is a string of length 8, with 7 bytes representing 7 characters of "abhijit" and one '\0' byte. b is an array of size 8, with 7 bytes representing 7 characters of "abhijit" and one '\0' byte. sizeof(b) and sizeof(d) are 16 bytes declaration of b is syntactically wrong
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.