char* A; char*b strcpy (A, b) error

Source: Internet
Author: User

The strcpy () function was suddenly used today

So

char* A = "ABCDEFG";

char* B = "GGGG";

strcpy (A, b);

Compile pass, but run unexpectedly directly ended, error;

Why is it?

Because char * A = "ABCDEFG";

"ABCDEFG" This is a const property, then it exists in the static storage area, a pointer to the static store, but cannot change its value

So you can't use the strcpy function

and char a[] = "ABCDF";

Char b[] = "CDs"

strcpy (A, b);

can achieve the desired effect, because

Char a[] = "abcdef"; this "abcdef" is still in the static store, but there will be a copy used to initialize the array a on the stack;

So can be modified!

A lot of knowledge knocking code encountered errors will learn, the above reasons used to know the knowledge point, but still write the wrong code, wrong to think why wrong, and then think of the knowledge points learned;

So write more code!!! Learn from your mistakes.

char* A; char*b strcpy (A, b) error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.