Machine-translated by Google. Questions are authored in English.
Top

Discussion

Point out the error in the following program.

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "Learn through IndiaBIX\0.com",  str2[120];
    char *p;
    p = (char*) memccpy(str2, str1, 'i', strlen(str1));
    *p = '\0';
    printf("%s", str2);
    return 0;
}

 

  • A.Error: in memccpy statement
  • B.Error: invalid pointer conversion
  • C.Error: invalid variable declaration
  • D.No error and prints "Learn through Indi"

Answer: D

Correct answer: No error and prints "Learn through Indi"

memccpy copies bytes until the given character has been copied, and returns a pointer to the byte after it - or NULL if the character never appeared. The first lower-case 'i' in the string is the one in Indi, so the copy stops there, the returned pointer is used to write the terminating null, and the result is Learn through Indi.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.