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;}
Question
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;}
Solution
At the point marked /* A */, the variables that are strings or pointers to strings are:
-
a: This is a string. It was declared as a character array and then populated with the string "hello" usingstrcpy. -
b: This is technically a string because it was declared as a character array and populated with individual characters that form the string "hello". However, it's important to note that this string is not null-terminated, which is a typical characteristic of strings in C. -
d: This is a pointer to a string. It was declared as a character pointer and then pointed to the string literal "hello". -
e: This is a string. It was declared as a character array and then populated with user input usingscanf. -
c: This is not a string. Although it was passed to the functionfwhich attempted to assign the string "hello" to it, this assignment does not affectcin themainfunction due to C's pass-by-value semantics. Therefore,cremains uninitialized at /* A */.
Similar Questions
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;}
If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable? ans. & . -> *
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"
Input: Which of the following statements is correct about the point in the program marked by the comment WWWW: double x; char y; int z; int main(int argc, char *argv[]) { double y; char z; /* WWWW */ return 0; } There are exactly two variables of type int in scope There are exactly two variables of type char in scope There are exactly two variables of type double in scope There is exactly one variable of type double in scope There are no variables of type int in scope
What is the output of the following C code?char *ptr;char mystring[] = "abcdefg";ptr = myString;ptr += 5;
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.