Beginner's entry record of two methods (arrays and pointers) about string representations in 1--c language

Source: Internet
Author: User

Now you need to represent a string "itcast", which means the method is listed as follows:

Method 1:

Char name[]= "Itcast"; At this time, the definition is a string variable, the array contains 6 letters and ' s ' a total of 7 elements, stored in the memory of the stack, the characters inside the array can be arbitrarily changed;

(Note: The memory of the "stack" is stored in a local variable, the value can be arbitrarily changed)

name[0]= ' t ';

printf ("%s\n", name);

The output result is: ttcast;

Method 2:

Char *name= "Itcast"; A string constant is defined, and the pointer variable name points to the address of the first character of the string ' I ', and the string is stored in a constant area.

A constant string cannot be changed, and the string will be cached for easy next use

printf ("%s\n", name); %s starts the output from the first character of the string pointed to by the pointer variable name, until it finishes reading to '.

The output is: itcast

The difference between the two:

When you use an array to redefine the same string char name2[]= "Itcast", the memory needs to open a new address for you to store the string;

And when you define the same string char *name2= "Itcast" with the pointer, the address value stored by name2 is the same as name1, which also points to the address stored in the constant area "Itcast", and the memory will not open up a new memory space for "Itcast". is to call the cache directly.

The situation used for both:

1. When your string needs to be changed frequently, use an array to represent the string;

Char name[]= "Itcast";

2, when you need to call this string and the string content is not modified, you can use pointers to represent, which can reduce the use of memory;

Char *name= "Itcast";

PS: This is determined to learn iOS development to write the first technical blog, although the content is very basic, but also take the first step, for their own future refueling it! Thank you for the chicken soup wen Tiny and take me to get started small three comrade. 2015.12.29

Beginner's entry record of two methods (arrays and pointers) about string representations in 1--c language

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.