The method of accessing a string of characters in the language c

The first: the standard method.

#include <stdio.h> 
#include <the iostream> int main () {
     // In this form, a character string is actually an array of char str1 [] = " Hello World " , str2 [ 30 ]; 
    the printf ( " D% \ n- " , str1); // get the address of the first element of the array of characters 
    the printf ( " % S \ n- " , str1);
     int I;
     // can be copied using the array index method, * (str1 + i) represents the index to the value of i, i.e. str1 [i] for (i = 0 ; * (str1 + i) =! ' \ 0 ' ; i ++ ) 
    { * (str2 + i) = * (str1 + i) ;



    
    
         
    }
    *(str2 + i) = '\0';
    printf("%s\n", str2);
    system("pause");
    return 0;
}

The second: pointer method.

#include <stdio.h> 
#include <the iostream> int main () {
     // In this form, a character string is actually an array of char str1 [] = " Hello World " ;
     char Str3 [ 30 ];
     char * P1, * P2; 
    P1 = str1; 
    P2 = Str3;
     for (; * P1 =! ' \ 0 ' ; * P1 ++, * P2 ++ ) 
    { * P2 = * P1; 
    } * P2 = ' \ 0 ' ; 
    the printf ( " S% \ n- "



    
        
    , str3);
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12121400.html