The difference between a C-type string and a character array

Source: Internet
Author: User

1, the string and the character array difference, the character array and the ordinary array, each element inside is a character, the string is in the character array, the end of the string is filled with the "\" cutoff character.

Once you have defined a string, you get a pointer to the first element of the character array, and you can easily access any element in the string by adding a cutoff at the end.
A string of type C is in the existence of a character array, which is the end of the string, which is used to mark the end of the string.
const * p; P is a constant pointer

2, the difference between the array name and the pointer

int a [20];

int *p =a;

(1) The connotation of the array name is that its reference entity is a data structure , and this data structure is an array; so sizeof (a) = 80;

(2) The extension of the array name is that it can be converted to a pointer to its reference entity, and is a pointer constant ;

(3) A pointer to an array is a different type of variable (under the WIN32 platform, with a length of 4), which means only the storage address of the group.

3, the advantages and disadvantages of arrays and linked lists, when to use arrays, when to use the linked list?

(1) The array is continuously stored in memory, through the array subscript can quickly access any element, its time complexity is O (1), the list in memory is not linear storage, access to any one of the elements need to start from the beginning of the traversal, Time complexity O (n);

(2) The array needs to determine the size beforehand, will cause memory waste, linked list is simpler, easy to manage;

(3) The insertion and deletion operations in the array are complex and require a large number of elements to be moved, and the insertion and deletion of the linked list is simple, just change the pointer point.

(4) The array operation is simple, the list is complex, but the function is powerful, the freedom is high.

#include <iostream>#include<CSTRING>using namespacestd;intLengh (Const Char*str);intMain () {//Define a stringChar*str ="The price is five pounds";Char*p =str; while(*p++) {cout<< *p;} cout<<"\nthe length of STR is:"<<Lengh (str);return 0;}//the length of the statistic stringintLengh (Const Char*str) {Const Char*p=str;intn=0; while(*p++) {n++;}returnN;}


The difference between a C-type string and a character array

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.