Describe the mistake in this code snippet
char* x;
strncpy(x, “Hello”, strlen(“Hello”)+1);
—————————————————————-
Describe the mistake in this code snippet
char* x = “Hello!”;
strncpy(x, “Hello.”, strlen(“Hello.”)+1);
—————————————————————–
Describe the mistake in this code snippet
char* x = “This is a test”;
char* y = “This is another test”;
char* token = strtok(x, “ “); // Expecting ”This”
char* token2 = strtok(y, ” ”); // Expecting “This”
char* token3 = strtok(x, ” ”); // Expecting “is”
Expert Answer
char* x;
strncpy(x, “Hello”, strlen(“Hello”)+1);
memory is not allocated for *x which may cause segmentation fault!
——————————————————————————————————————-
char* x = “Hello!”;
strncpy(x, “Hello.”, strlen(“Hello.”)+1);
The function will try to copy 6+1 characters in a space of 5+1 char
———————————————————————————————————————-
char* x = “This is a test”;
char* y = “This is another test”;
char* token = strtok(x, “ “); // Expecting ”This”
char* token2 = strtok(y, ” ”); // Expecting “This”
char* token3 = strtok(x, ” ”); // Expecting “is” /*
char* token3 = strtok(NULL, ” ”); // Expecting “is” /* this is correct
*/
Will not provide the expecting outout”is” since we reset strtok by sending a (new) string to the method. If we had passed NULL instead of x, we would have gor that result