当前位置:网站首页>C language string and pointer - learning 25

C language string and pointer - learning 25

2022-06-12 00:39:00 XG. Solitary dream

This paper is finally updated at 2022 year 02 month 20 Japan , Has more than 7 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !

How strings are referenced

  • Strings are stored in character arrays .
  • Reference a string , There are two ways to do this :
    • use A character array Store a string , Can pass Array name and format declaration “%s” The output string can also refer to a character in the string through the array name and subscript .
    • use Character pointer variable Point to A string constant , adopt Character pointer variables refer to string constants .
  • Example
#include <stdio.h>
void main() {
    char string[] = "I love China!";
    printf("%s\n", string);
    printf("%c\n", string[7]); //  Output eighth character 
}

// Output a string through a character pointer 

#include <stdio.h>
void main() {
    char *string = "I love China!";
    printf("%s\n", string);
    printf("%c\n", string[7]); //  Output eighth character 
}

take a The string array is copied to b Array of strings

#include <stdio.h>
void main() {
    char a[] = "abcdefg";
    char b[20];
    int i;
    for (i = 0; *(a + i) != '\0'; i++) {
        *(b + i) = *(a + i);
    }
    *(b + i) = '\0';
    printf("a = %s\n", a);
    // printf("b = %s\n", b);
    printf("b = ");
    for (i = 0; b[i] != '\0'; i++) {
        printf("%c", b[i]);
    }
    printf("\n");
}

Handle with pointer variables

#include <stdio.h>
void main() {
    char a[] = "abcdefg", b[20],*p1,*p2;
    p1 = a;
    p2 = b;
    for (; *p1 != '\0'; p1++) {
        *p2 = *p1;
        p2++;
    }
    *p2 = '\0';
    printf("a = %s\n", a);
    printf("b = %s\n", b);
}

Comparison of character pointer variable and character array

  • use A character array and Character pointer variable Can achieve String storage and operation ; There is a difference between them , There are mainly the following points :
    • 1. A character array from Several elements form , Put one character in each element ; and Character pointer variable Where is the Address ( String number 1 A character address ), Instead of putting strings in character pointer variables .
    • 2. Different assignment methods , You can assign values to character pointer variables , But you cannot assign values to array names .
      • char *a;a="I love Chinal"; correct
      • char str[14];str[0]='1'; correct
      • char str[14]; str = "I love China!"; error
    • 3. The contents of the storage unit are different , Compiled as A character array Distribute Several storage units To store the values of each element , And yes Character pointer variable , Just assign One storage unit .
      • char *a, str[10];a=str;scanf ("%s",a); correct
      • char *a; scanf("%s",a); error
    • 4. Pointer to the variable The value of is You can change Of , and Array name Represents a fixed value ( That is, the address of the first element of the array ) Can't change .
    • Example
#include <stdio.h
void main() {
    char *a = "I love China!";
    a = a + 7;
    printf("%s\n", a); //  Output from the eighth character 
}
  • 5. A character array in The value of each element can be changed Of , but Character pointer variable Point to the The contents of string constants cannot be replaced Of .
    • char a[]="House", *b="House";
    • a[2]='r'; correct
    • b[2]='r'; error
  • 6. Reference array elements
    • For character arrays, you can use The subscript method and Address method Reference array elements .
      • for example :a[5]*(a+5)
    • If the character pointer variable p=a, You can also use Pointer variable with subscript Form and Address method quote .
      • for example :p[5]*(p+5)
  • 7. use The pointer variable points to a format string , You can use it Instead of printf Function Format string .
  • Example
#include <stdio.h>
void main() {
    char *format; 
    int a = 10;
    float b = 3.6;
    format = "a = %d, b = %f \n";
    printf(format, a, b);
    //  amount to 
    printf("a = %d, b = %f \n", a, b);
}

Character pointer as function parameter

  • If you want to put the A string from A function “ Pass on ” To Another function , It can be used Address delivery The way to , The box Character array name do Parameters , It can also be used. Character pointer variable do Parameters .
  • stay Called In the function of Change the contents of the string , stay The main function Medium can Reference the changed string .
  • Example

Using function call to copy string .

#include <stdio.h>

void main(){
    //  Character array names are used as parameters 
    void string_copy(char from[], char to[]);
    char a[] = "abcdefg", b[20];
    string_copy(a, b);
    printf("a = %s\n", a);
    printf("b = %s\n", b);
}

void string_copy(char from[], char to[]){
    int i;
    for (i=0; from[i]!= '\0'; i++) {
        to[i] = from[i];
    }
    to[i] = '\0';
}
#include <stdio.h>

//  Character pointer variables are used as parameters 
void main(){
    void string_copy(char *from, char *to);
    char *a = "abcdefg", b[20];
    char *to = b;
    string_copy(a, to);
    printf("a = %s\n", a);
    printf("b = %s\n", b);
}

void string_copy(char *from, char *to){
    for (; *from != '\0'; from++,to++) {
        *to = *from;
    }
    *to = '\0';
}

Pointer to function

  • stay C In language , A function always occupies a contiguous area of memory , and Function name Is the memory area occupied by this function The first address .
  • We can give this The first address ( Or entrance address ) Give a pointer variable , Make the pointer variable point to the function .
  • And then through Pointer to the variable Can Find and call this function . We call the pointer variable of this number “ Function pointer variable ”.

Call function with function pointer variable

  • Definition of function pointer variable
    • The general form is :
      • Type specifier (* Pointer variable name )()
    • Type specifier : Represents the type of the return value of the referred function
    • * Pointer variable name :“*” The following variables are defined pointer variables
    • (): A pointer variable refers to a function
  • for example :int (*pf)(); Express pf Is a pointer variable to the function entry ,( Function value ) Integer. .
  • Example
#include <stdio.h>

//  Character pointer variables are used as parameters 
void main(){
    int max(int, int);
    int(*p)(int, int);
    int a, b, c;
    p = max;
    scanf_s("%d %d", &a, &b);
    c = (*p)(a, b);
    printf("max=%d\n", c);
}

int max(int x,int y){
    int z;
    z = (x > y) ? x : y;
    return (z);
}
原网站

版权声明
本文为[XG. Solitary dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011445163564.html