Array is not a string

Source: Internet
Author: User

In the C language we know that there is no string such data type, so in order to represent the string, we will generally use a char type array to represent, which leads to the beginning when we think the array and string when the same, in fact, the array is an array, string is a string;

Array is an alias of the fixed memory block size, which is a customizable memory size data type;

We know that strlen can figure out the number of data in a string, and sizeof can figure out the amount of memory the variable occupies.

  

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 5 voidMain ()6 {7     CharA[] = {'a','b','C','D','e'};//a one-dimensional array, not a string ending with ' /',8     CharA_1[] ="ABCDE";//string, allocated six byte space9 Tenprintf"sizeof (a):%d,strlen (a):%d\n",sizeof(a), strlen (a));//5 strlen (a) > sizeof (a) Oneprintf"A_1:%d,strlen (a_1):%d\n",sizeof(a_1), strlen (a_1));//6 5 Aprintf"a:%s\n", a);//without the ability to automatically end, will print out ABCDE and a bunch of garbled -printf"a_1:%s\n", a_1);//Print out ABCDE -  theSystem"Pause"); -}

From these lines of code we can find that the same is the import of a one-dimensional array Abcde,char a[] = {' A ', ' B ', ' C ', ' d ', ' E '} is an array, char a_1[] = "ABCDE", then what is the difference between an array and a string?

We can see: The number of data in the array is greater than the number of allocated space, strlen This function, the statistics of the number of character data encountered when null or ' s ' will stop, so that is, in-memory garbled will be considered to be deposited data;

However, the string will be implicitly written to a ' "", that is, the string terminator, so its length is 6, the length of the deposited data is 5;

So then someone would want to write char a[] = {' A ', ' B ', ' C ', ' d ', ' e ', ' n '}; plus a 0 it's good to see the words, but as we said earlier, there is no string in C syntax, in order to show that we usually use arrays to represent,

So the question is, may I ask 2.0000 = 2 Does this say that 2.0000 and 2 are the same? Of course not, although their values are equal. But one is a decimal, one is an integer, the types are different, although equality does not mean that they are the same data type;

Array is not a string

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.