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?

  1. Error
  2. Helloworld
  3. NuII,Helloworld
  4. None of the above

Recommended Answers

All 5 Replies

This is clearly a homework or interview question, so please let us know what your thoughts are first.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.