C language char character string detailed appendix video

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Video to explain:


Video recorded their, think this video to explain a little vague, and nonsense, error -, and many have left, so this text interpretation.

This paper is mainly on the character and string memory allocation, constant string representation, pointer, data conversion of the content.

The 1 code
  ASCII can store all the Latin characters, a character takes 1 bytes
  ANSI ANSI code can be regarded as a kind of extended ASCII codes, including ANSI and GB2312, BIG5, JIS coding standard (in simplified Chinese system, ANSI encoding to represent the GB2312 code, in the Japanese operating system, ANSI encoding to represent the JIS code).
           ANSI code can be stored Latin characters and non Latin characters, GB2312 encoding a Latin characters occupy 1 bytes, a Chinese characters is equivalent to two Latin characters occupy 2 bytes.
           A plain text file including the code file (.C.Cpp.Java.Html), is generally based on ANSI code storage.
Unicode is also called Unicode, Unicode, it can be stored for use by all countries in the world of words, each character occupies 2 bytes, whether Latin characters or non Latin characters.
  In the C language to express a character or string constant is Unicode code, you can use a macro: _T ("") defined on: tchar.h, Unicode encoded data can be stored using the wchar_t wide character data type.Wchar_t data type is generally 16 or 32 bits, different C or C + + library has different rules.

2 all strings are'2 所有Character string均以 '\0’ 结尾&rsquo to end;
  In the C language, so at the end of the string in double quotes are the default with a In theC语言当中,So的In a string enclosed in double quotes.的末尾都默认加上了一个 \0..
  Such as "abcde” is actually abcdeSuch as "abcde” 实际上是 abcde\0 ..
  \0 at the end of a string, actual also occupied a byte.
  It is necessary to pay attention to, will be the default plus 要注意Is,只有在字符串中才会默认加上 \0,Such as 'a' 像以Single quotation marks单个字符的后面是不会加 \0 的,而 "a" 这样用双引号括起来的不管是一个字符Or几个字符,都会加上 \0. only in a string, such as'a'like to back a single character single quotes is not 要注意的是,只有在字符串中才会默认加上 \0,如 'a' 像以单引号括起来的单个字符的后面是不会加 \0 的,而 "a" 这样用双引号括起来的不管是一个字符还是几个字符,都会加上 \0., and "a" with double quotation marks is a matter of a character or several characters, will add 要注意的是,只有在字符串中才会默认加上 \0,如 'a' 像以单引号括起来的单个字符的后面是不会加 \0 的,而 "a" 这样用双引号括起来的不管是一个字符还是几个字符,都会加上 \0..
3 ‘ Single quotation marks and "double quotation marks represents values
  char ch = ‘A’; The ' 单引号括起来的,In essence is a representative for range0~255Integer data,The integer value corresponding to the characters in theASCIIInteger data corresponding to the codes in the(Detailed visibleMSDN-ASCII Character Codes).char Is an integer data,Unlike other integer data is,char The default is unsigned data,Occupy1Byte,The data size is in the range of0~255.Such as char ch = 65; Here we can directly assign an integer data to char A type variable ch.If we useprintf(“%c\n”, ch); So whether it is assigned toch的是 'A' 还是 65(65 在ASCIICode corresponding to the character of uppercase lettersA),The output is A.Of course, we also use %d The integer data output.
   Example:
#include <stdio.h>
 
int main(void)
{
char a = 65;
char b = 'A';
printf("%c, %d\n", a, a);
printf("%c, %d\n", b, b);
 
return 0;
}
/*
Reference output:
A, 65
A, 65
*/
 

   char *string = “abcd”; Among them,We will be able to "abcd" Assignment to a character pointer is: 以双引号括起来的字符串,The value of the expression is a pointer.In this statement,字符串 abcd\0 Is a constant.When the program runs automatically judging whether memory has been deposited. abcd\0 This data,If a double quotation marks are deposited on behalf of the first element in the string.,No storage is created.所以 "abcd" The double quote expressions represent the pointer points to the first element in the string. a.Because the string abcd\0 Is a constant,So we can't string To modify its value,But we can go to modify string Point to address,如: string = “efg”; Here "efg" And "abcd" The concept is the same.
#include <stdio.h>
 
int main(void)
{
char *str = "abcd";
printf("%s, %#x\n", str, str);
 
return 0;
}
/*
Reference output:
abcd, 0x40c000
*/

4 char[] The character array
    char string[5] = “abcd”; This statement is the constant abcd这个语句表示的是将常量 abcd\0 复制到所分配的5字节内存空间中去,与char *string = “abcd”;不同之处就在于,前者是将一个内存区域中的数据复制到另外一个内存区域里去,后者是定义一个Pointer让它指向了这一个内存区域. replication to the allocated 5 bytes of memory space, &ldquo and char = *string; abcd”; the difference is, the former is an area of memory data is copied to another memory area, the latter is a pointer that points to the definition this memory area.
    Thorough understanding of an array of friends should also know, no matter what the array, the array name is the definition of a pointer. However, this pointer is different from the ordinary pointer is a pointer, the pointer address cannot be changed, string is a constant. The pointer to the first element of the array in this program, a pointer to the array. In the char string[5] = “ abcd” string;, refers to the first element is the abcd深入了解过数组的朋友应该也知道,不管是什么数组,定义的数组名其实都是一个指针.不过这个指针不同于普通指针的是,这一个指针指向的地址不能被改变,指针string是一个常量.这个指针指向了数组的第一个元素,程序以这一个指针来找到整个数组.在 char string[5] = “abcd”; 中,string 就指向了第一个元素的地址,也就是 abcd\0 中 a 的地址. address, a address.
     Even if we can change string Value is not reasonable,Because string Point is not constant region abcd\0 ,But the program of your allocated memory space into the abcd\0 指针,If we modify string The value of the,We cannot find the memory address where the string variable..And if we don't call external functions to change achar [] String value.,Only by subscript a a modified values for each character can modify the entire string,如: string[0] = ‘e’; string[1] = ‘f’; …… Of course, if such modification will be very cumbersome,So we can use aCTo provide us with language char String copy function: strcpy.
     strcpy The function prototype: char*strcpy(char*strDestination,constchar*strSource );
               The parameter 1:strDestination to the target string first.;
               The parameter 2:strSource            to the first address of the source string.
               Returns: returns a pointer to strDestination.
      For the data type const char * on visible the next chapter.
      The function is to copy the data in the strSource to the strDestination, it returns a value we generally does not use, can be ignored. Functional equivalence of this function in the char string[5] = “ abcd”; in = number, but is different with the whole statement is, strcpy does not allocate new memory.
      Function uses the example: change the value of string for EFG:
                             strcpy(string, “efg");
      More char character string manipulation functions can refer to MSDN-String Manipulation Routines
      Array properties of char arrays and other data types, can also increase so that the modified element values through a point to the first element is pointer:
char string[5] = “abcd”; 
char *pChar = string;
*pChar = ‘e’; // Modify the first element
*(++pChar) = 'f'; // Modify the brackets not essential second element here, just for convenient observation


5 const char * and char *const
      const char * a; This statement defines a constant pointer to a, which is a pointer to a constant, meaning that cannot be modified by a it points to the memory address value. Note here is not through the A to modify it to address in value, does not mean that a can only to a constant and cannot refer to variables. After only a to variables this variable, cannot be modified by a to the char const *a value. In addition; statement, equivalent.
      char *const b; This statement defines a pointer constant B, also is the constant a pointer, meaning that B is a constant, the value of B cannot be changed, namely point to address of the B cannot be changed, can pass the B to modify it to the value in memory. Here also note, B can refer to constant also can point to variables, just say B to who cannot be changed. If the B points to a constant, then of course through the B to modify the value of the constant is wrong. Because here is defined as a constant variable, so must be defined in time to the assignment.
      Example:
const char *a = "abcd"; // OK
char str[5] = "abcd"; // OK
char *const b = str; // OK
const char *c = str; // OK
 
a = "fff"; // OK
*a = 'f'; // Error cannot pass through the constant pointer a to modify it to the value in the memory address
b = "abcd"; // Error pointer constant B is a constant, cannot be changed
*b = 'f'; // OK
*++b = 'f'; // Error + + increment will make changes to the pointer constant b value
b[1] = 'f'; //OK
*c = 'f'; // Error cannot modify it to C through the constant pointer memory address value, although its memory address points to data in variables

      If we want to use the malloc function to an array distribution of memory space, and let it and define an array, pointer to the first element of the value cannot be changed, then we can define:
      char *const string = (char *)malloc(5);
Conversion of 6 characters, strings and numerical.
      int i = (ch-48); This statement is the single character ch into digital and deposited in the integer variable I function.
                            Because the 0 characters in ASCII code corresponding to the 48, behind the values also increased by 1, so the integer form with its corresponding ASCII code minus 48 is the single character.
                              So, we can also turn, the integer is converted into characters: char Ch = (i+48);
      Conversion between strings, we cannot at the same time, all the characters in a string conversion, no external function call it, we can use this property to string a conversion, the code example:
#include <stdio.h>

int StringInt(const char *str) // Converts a string into an int type and return
{
    int val = 0;
    int index = ( strlen(str) - 1 ); // The index value
    int pn = 1; // That is positive or non positive, positive number is 1, the non positive number is 0
    int f = 1, i = 1; // For subsequent cycles
    const char *pChar = str;
    
    if ('-' == *pChar) { // Judging the first element is a string
        index--;
        pn = 0;
        *pChar++;
    }
    
    while (index >= 0)
    {
        f = 1;
        for (i=1; i<=index; i++)
            f *= 10;
        val += ( (*pChar++) - 48 ) * f;
        index--;
    }
    if (0 == pn)
        val *= -1; // Conversion is negative
        
    return val;
}

int main(void)
{
    printf("%d\n", StringInt("333"));
    printf("%d\n", StringInt("0"));
    printf("%d\n", StringInt("-0"));
    printf("%d\n", StringInt("-321"));

    return 0;
}
/*
Reference output:
333
0
0
-321
*/

      The StringInt function in the code can convert a string into an integer data, and return, if each one of us is going to write a function to the conversion of the then will waste a lot of time, gives us the following functions in the C language, it is very easy to let the conversion is complete numerical and inter character
      The string -> numerical simulation:
      intatoi(constchar*string ); A string conversion to return type, and the code of StringInt identical.
      doubleatof(constchar*string ); To convert a string to double precision floating point data and returns
      __int64 _atoi64(const char*string ); A string with a 64 bit integer
      longatol(constchar*string ); To convert a string returns a long integer
      The -> string value;:
      char*_itoa(int value,char*string,int radix ); An integer value is converted to a string using Radix hexadecimal representation of data stored in the string and returns a pointer to string.
      char *_i64toa(__int64 value,char*string,int radix ); Ibid, here is a value data type is different, the same function.
      char*_ui64toa(unsigned_int64 value,char*string,int radix ); Ditto.
      wchar_t*_itow(int value,wchar_t*string,int radix ); An integer value is converted to a string using Radix representation of data stored in a wide string string and returns a pointer to string.
      wchar_t*_i64tow(__int64 value,wchar_t*string,int radix ); Ditto.
      wchar_t*_ui64tow(unsigned __int64 value,wchar_t*string,int radix ); Ditto.
      The conversion function C language data more available on the MSDN-Data Conversion
      Formatting data manipulation functions sprintf and sscanf :
      int sprintf(char*buffer,constchar*format [, argument] ... ); Format the storage data to a string. The first parameter is the buffer zone, the second is the format control string, third is the output table columns, second of third parameters and printf in the first second parameters used are the same. The return value is the length of the string stored in the buffer. Note, sprintf is enough to use the following conversion character:
      %% Print the percent symbol, not conversion.
      %C integer into the corresponding ASCII character.
      %D integer to decimal.
      %F double precision numbers into a floating point number.
      %O integer eight into.
      %S integer into a string.
      %X integer converted to lowercase sixteen carry.
      %X integer converted to uppercase sixteen carry.
      int swprintf(wchar_t*buffer,constwchar_t*format [, argument] ... ); Same as sprintf, but swprintf can use all of the conversion character
      Usage examples:
#include <stdio.h>

int main(void)
{
   char str[6] = "abc";
   int i = 3;
   sprintf(str, "%s,%d", str, i);
   printf("%s\n", str);

    return 0;
}
/*
Reference output:
abc,3
*/

      intsscanf(constchar*buffer,constchar*format [, argument ] ... ); The string buffer to format the partition in the format list data. The first second parameters, third parameters and the usage of second sscanf. Sscanf also like sprintf, use only the partial conversion of the same characters.
      Sscanf supports set operations:
      %[a-z] Said a to Z in the match any character, greedy (as much as possible matching)
      %[aB'] Matching a member of the A, B, ', greedy
      %[^a] Matching non arbitrary character of a, and stop reading, greedy
      intswscanf(constwchar_t*buffer,constwchar_t*format [, argument ] ... ); Same as sscanf, you can use all the conversion character
      Usage examples:
#include <stdio.h>

int main(void)
{
int a, b;
char str1[10], str2[10], str3[10];

sscanf("123, 456", "%d, %d", &a, &b);
printf("a = %d, b = %d\n", a, b);

sscanf("abchhdad 123 oofg", "%[a-z] %[1]", &str1, &str2);
printf("str1 = %s, str2 = %s\n", str1, str2);

return 0;
}
/*
Reference output:
a = 123, b = 456
str1 = abchhdad, str2 = 1
*/


        Detailed usage about sscanf can be found in Baidu Encyclopedia: http://baike.baidu.com/view/1364018.htm
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kerwin at November 15, 2013 - 8:25 PM