C Language Learning 018: strdup copying string array, 018 strdup

Source: Internet
Author: User

C Language Learning 018: strdup copying string array, 018 strdup

In C Language Learning 005: the strings that cannot be modified are stored in the constant area. Assigning a value to the array actually copies the strings in the constant area to the stack memory, if we assign this array to the pointer, we can change the elements in the array, as shown below

1 int main () {2 char s [] = "hello c"; 3 char * temp = s; 4 temp [0] = 'a '; 5 temp [1] = 'B'; 6 printf ("% s \ n", s); 7 return 0; 8}

But now we don't want the pointer to modify the elements in the string array, but we can get the elements in the string, so we need to copy a copy of the elements in the string array, then, the address is given to the pointer, which can be implemented through strdup.

1 int main () {2 char s [] = "hello c"; 3 char * temp = strdup (s); 4 temp [0] = 'a '; 5 temp [1] = 'B'; 6 printf ("% s \ n", s); 7 return 0; 8}

Related Article

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.