Char* and char[]

Source: Internet
Author: User

Char *s1= "Hello"; Point to Constant area

Char s2[]= "Hello"; Memory space pointing to an array

Char *s1 's S1 is a pointer to a piece of memory area, the size of the memory area it points to can change at any time, and when the pointer to a constant string, its contents can not be modified, otherwise at run time will be error.
Char s2[] s2 is an array corresponding to an area of memory, whose address and capacity will not change in the lifetime, only the contents of the array can be changed.


char s2[] = "Hello"; "


s1=s2;  //OK

s2=s1//Error

also when using strcpy strcpy (S2,S1) is possible, and because S1 is T-pointing character constants cannot be modified, strcpy (S1,S2) is not able to pass.

when Char*s1=s2, the compiler automatically converts to CHAR*S1=&S2, which is equivalent to two.

The two pointers are assigned a different value

Char*s1="Hello"; Char s2[]="World"; Char **s3=&S1; // Char **s4=s2;   ERRORchar *s4=S2; Char **s5=&s4;cout<<*s3<<""<<*s5;

The output "Hello world", char[] will be assigned in two steps.

Returns the difference between char* and char[] in a function:

Returns char* points to a constant area that can continue to exist after the function has ended.

char[], stack function end recycle, return garbled, to return can be declared inside the function with static char[]

Char* and char[]

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.