when we use pointers in predefined string functions why is gives runntime error.the c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amantripathi
    New Member
    • Aug 2013
    • 1

    #1

    when we use pointers in predefined string functions why is gives runntime error.the c

    Code:
    #include<stdio.h>
    void main()
    {
    	char *a="your";
    	char *b="name";
    	printf("%s",strcpy(a,b));
    }
    Last edited by Rabbit; Aug 17 '13, 05:19 AM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are copying string b to string a.

    Unfortunately, string a is a literal which means the string is const. The compiler will not let you change the value of a literal.

    Change the program to use arrays:

    Code:
    char a[] ="your";
    char b[] ="name";
    etc...
    BTW: strcpy assumes the destination array is big enough to hold the source string. It's up to you to be sure this is true.

    Comment

    Working...