Consider the following C code snippet:
char *sentence = NULL;
strcpy(sentence, "Helloworld");
printf("%s", sentence);
is the code segment correct or error.
If correct what is the output?
- Error
- Helloworld
- NuII,Helloworld
- None of the above
Consider the following C code snippet:
char *sentence = NULL;
strcpy(sentence, "Helloworld");
printf("%s", sentence);
is the code segment correct or error.
If correct what is the output?
Jump to PostThis is clearly a homework or interview question, so please let us know what your thoughts are first.
Jump to PostThe pointer just points to NULL. It has no storage space. you need to allocate space. You could use strdup to do that. IE,
sentence = strdup("helloworld");
strdup will allocate space for the string (including terminating NUL character) and copy the string to the newly allocated space that "sentence" now …
This is clearly a homework or interview question, so please let us know what your thoughts are first.
Why don't you just try it and see for yourself?
i tried. it says runtime error
The pointer just points to NULL. It has no storage space. you need to allocate space. You could use strdup to do that. IE, sentence = strdup("helloworld");
strdup will allocate space for the string (including terminating NUL character) and copy the string to the newly allocated space that "sentence" now owns.
i tried. it says runtime error
Then you have your answer. Wouldn't that have been easier than asking us to do your work for you?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.