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