A detailed description of Char *str1 and Char str2[]

Source: Internet
Author: User

A knowledge point of C language:

char *str1 = "Hello World" and char str2[] = "Hello World"; The difference:

1, Memory management:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/81/FE/wKioL1dG2zTB2Ve-AAAkFh2oxes797.png-wh_500x0-wm_3 -wmp_4-s_1439604303.png "title=" Qq20160526191257.png "alt=" Wkiol1dg2ztb2ve-aaakfh2oxes797.png-wh_50 "/>

Memory is divided into: read-only code snippet, read-only data segment, BSS, heap, stack five different regions.

As shown: read-only code snippets are generally stored in well-written programs.

Read-only data segments are constants (string constants, array names, function names), and read-only areas cannot be changed!

The BSS region holds global variables and static class variables that are uninitialized, have values of 0, and are initialized.

The heap is in malloc (), calloc () ... Application space, not by the operating system recycling, must rely on the programmer himself through the free () Reclaim space, or memory leaks!!!

The stack is stored in a local variable, parameter variables, the stack space can be used by any function, is the reliability of the data is very low!

2, STR1 is the pointer variable, str1++; Right, pointer plus minus integer, result or pointer.

For str2, char str2[] = "Hello World"; <=> char str2[] = {' h ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ', ' n '};

This is the initialization of the character array, so str2 is the array name (pointer constant), Str2++;false, constant cannot be assigned value!

the "Hello World" string itself is an array of characters ending in 0!!!       

3, "Hello World" string constant in the read-only data segment, the value itself content cannot be changed!!!

str1 is a pointer variable, *STR1 = ' t '; False to change the range in a read-only data segment.

STR2 is an array name, its space is applied in the stack, *str2 = ' t '; True, the data in the stack can be changed.

4. A deep understanding of char *str1 = "Hello World"

Strlen (STR1); 11

Strlen (STR2); 11

sizeof (STR2); 12 array name, number of bytes, is the number of bytes of space requested by the array, on the 0 end flag.

sizeof (STR1); 4/8 platform-related, 32-bit system, pointer 4-byte, 64-bit, pointer 8-byte

sizeof (*STR1); 1 *str1 is ' h ', which occupies one byte.

sizeof ("Hello World"); 12 space occupied by string constants

Strlen (); the function, calculated as the string length, does not count that 0;sizeof (); keyword, monocular operator, to calculate the number of bytes, the end of the string is the flag 0.

5, [] The value is determined by the number of initial values.

This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1783602

A detailed description of Char *str1 and Char str2[]

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.