Contact between the pointer and the string

0. Common Mistakes

  1. c No language string data type base

    C language has <string.h>this header file, so we granted that the C language has this type of string, string variable to declare this form of the following string aString; compilation fails . The original C language are they not string this type, so the strings are char array by storing, and <string.h>function prototypes in this header file declaration also full of all sorts of operations on char array . Until it appeared in the C ++ string class (I mean the class, not the type).

  2. When the string is defined by an array of characters, the default will be the last element in the array plus '\ 0' marks the end

  3. Given a second point, if the character array to distinguish between normal and strings, reference to the following codes

//此string1后面不会有'\0'标志,就是一般的字符数组
char string1[3]={'a','b','c'};

//string2为字符串形式,结尾有\0标志,长度为4
char string2[4]="abc"    

Note: the length of string2 here may not be limited, and the default is omitted, the compiler to automatically set the length, if necessary artificially set length, the need to take into account the '\ 0' character

If the character array length is not enough, the compiler error

D:\otherworkspace\devcppworkspace\ds\ds-c2\demo5.cpp [Error] initializer-string for array of chars is too long [-fpermissive]

  1. Relations array names and pointers

    Sample code is as follows:

    #include<stdio.h>
    
    int main(){
     int i;
     char string[]="abc";
     for(i = 0;i<4;i++){
         printf("%p\t",string+i);    
         printf("%p\t",&string[i]);
         printf("%c\n",string[i]);
     }
     return 0;
    } 

    Print results

    000000000062FE10        000000000062FE10        a
    000000000062FE11        000000000062FE11        b
    000000000062FE12        000000000062FE12        c
    000000000062FE13        000000000062FE13

    According to the results of running the code point of view, string [] is an array name is actually a pointer to the first element at index [0], we can pointer arithmetic directly, it automatically according to the type of pointer corresponding to the length of movement, for example, here is a character pointer, one byte, then the string ++, the cursor will automatically move back a byte.

String read 1.0

#include <stdio.h>
int main()
{
    char name[20];
    printf("Enter name: ");
    scanf("%s", name);
    printf("Your name is %s.", name);
    return 0;
}

operation result:

Enter name: Dennis Ritchie
Your name is Dennis.

scanf()Function to read the character sequence, when faced with a space (space, newline, tab etc.) to stop reading.

So how do you read them?

#include <stdio.h>
int main()
{
    char name[30];
    printf("Enter name: ");
    fgets(name, sizeof(name), stdin);  // read string
    printf("Name: ");
    puts(name);    // display string
    return 0;
}

Export

Enter name: Tom Hanks
Name: Tom Hanks

Here, we use the fgets () function to read a string from the terminal. fgets (name, sizeof (name), stdin); results sizeof (name) is 30. Thus, we can enter up to 30 characters as an input, i.e., the size of the name string can be specified into other length. In order to print the string, we use the puts (name), you can also use direct printf () function prints.

Note: gets () function can accept user input. However, it has been removed from the C standard. This is because gets () allows the input of characters of any length. Therefore, there may be a buffer overflow.

1.1 Get string length

Sample Code

//求字符串长度 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
    const char str[] = "How many characters does this string contain?";
 
    printf("without null character: %zu\n", strlen(str));
    printf("with null character:    %zu\n", sizeof(str) );
    return 0; 
}

output

without null character: 45
with null character:    46

Guess you like

Origin www.cnblogs.com/ericling/p/11761031.html