Difference between char C [] = "hello" and char * c = "hello" in the function -- C

Source: Internet
Author: User

Char C [] = "hello"

Is local data.

C [0] = 'T'; // OK


Char * c = "hello"

Global data, in the static storage area.

* C = 'T'; // false


# Include <stdio. h>/* Example 1 */const char * stra () {/* properly open a static bucket through static */static char STR [] = "hello "; /* If a local data is incorrectly allocated, the returned result is uncertain and insecure after the stack release of the function stops memory, possible recovery at any time */Char STR [] = "hello";/* correct allocation of a Global Array, global area of memory */char * STR = "hello "; return STR;}/* Example 2 */intmain () {static char str1 [] = "hello"; char str2 [] = "hello "; char * str3 = "hello"; char * str4 = "hello"; printf ("str1 = 0x % x \ n", str1 ); printf ("str2 = 0x % x \ n", str2); Printf ("str3 = 0x % x \ n", str3); printf ("str4 = 0x % x \ n", str3);/* symptom summary: 1. The addresses of str1, str3, and str4 remain unchanged. 2. The str3 and str4 addresses are the same. 3. The address of str2 is always variable. Cause: 1. The str1, str3, and str4 addresses are in the static storage space. 2. str3 and str4 are in the same static bucket as strings. 3. str2 is in the stack space. * //}/* [[Email protected] test_class] #. /. out str1 = 0x8049734str2 = 0xbf921e42str3 = 0x8048530str4 = 0x8048530 [[email protected] test_class] #. /. out str1 = 0x8049734str2 = 0xbfd174a2str3 = 0x8048530str4 = 0x8048530 [[email protected] test_class] #. /. out str1 = 0x8049734str2 = 0xbfa84cd2str3 = 0x8048530str4 = 0x8048530 [[email protected] test_class] #. /. out str1 = 0x8049734str2 = 0xbffd00002str3 = 0x8048530str4 = 0x8048530 [[email protected] test_class] #. /. out str1 = 0x8049734str2 = 0xbf954982str3 = 0x8048530str4 = 0x8048530 */


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.