首页 > 解决方案 > 用指针交换c中的字符串函数

问题描述

当我尝试在函数中的字符串之间进行交换时,Update_student它不会成功。为什么?

#include <stdio.h>
#include <string.h>

#define SIZE 1

struct student {
    int id_number;
    char name[50];
    char sex[6];
    int quiz_score[2];
    int total_score;
};

void Add_Student_Records(struct student *pupil) {
    printf("ID:");
    scanf("%d", &pupil->id_number);
    printf("Name: ");
    scanf("%s", &pupil->name);
    printf("Sex :");
    scanf("%s", &pupil->sex);
    for (int i = 0; i < 2; i++) {
        printf("Quit score %d:", i + 1);
        scanf("%d", &pupil->quiz_score[i]);
    }
    pupil->total_score = pupil->quiz_score[0] + pupil->quiz_score[1];
    return; 
}

void Add_Students(struct student *students) {
   for (int i = 0; i < SIZE; i++) {
        printf("Student %d:\n", i + 1);
        Add_Student_Records(students);
    }
    return;
}

void Print_Students(struct student *students) {
    for (int i = 0; i < SIZE; i++) {
        printf("Student %d details: \n", i + 1);
        printf("ID:%d\n", students->id_number);
        printf("Name:%s\n", students->name);
        printf("Sex:%s\n", students->sex);
        for (int i = 0; i < 2; i++) {
            printf("Quit score %d:\n", students->quiz_score[i]);
        }
        printf("Total score: %d\n", students->total_score);
        students++;
    }
    return;
}

void Replace_Strings(char **old_string, char **new_string) {
        *old_string = *new_string;
        return;
}

void Update_Student(struct student *students) {
    int i = 0;
    char name[50], new_name[50], cur_name[50];
    printf("You can update name, and scores.\n");
    printf(" current Name: ");
    scanf("%s", name);
    printf("new name: ");
    scanf("%s", &new_name);
    while (i < SIZE) {
        strcpy(cur_name, students->name);
        if (strcmp(cur_name, name) == 0) {   
            char *ptr_old_stud_name = students->name;
            char *ptr_new_stud_name = new_name;
            Replace_Strings(&ptr_old_stud_name, &ptr_new_stud_name);
        }
        i++;
        students++;
    }
    return;
}

int main() {
    struct student students[SIZE];
    char ch;
    /*1.Add student, 2. Print student*/
    printf("1.Add student\n2.Print students\n3.Update student\n");
    scanf("%c", &ch);
    while (ch != 'E') {
        if (ch == '1') {
            Add_Students(&students[0]);
        }           
        else if (ch == '2') {
            Print_Students(&students[0]);
        }
        else if (ch =='3') {
            Update_Student(&students[0]);
        }
        printf("Another operation:\t");
        scanf("%c", &ch);
    }
}

标签: cstringpointers

解决方案


Replace_Strings(&ptr_old_stud_name,&ptr_new_stud_name);ptr_old_stud_name将和的地址传递ptr_new_stud_nameReplaceStrings

ptr_old_stud_name并且ptr_new_stud_name是局部变量。第一个是已设置为指向 的指针students->name。第二个是已设置为指向 的指针new_name

Replace_Strings将传递给它的指针的第一件事更改为传递给它的指针的第二件事。所以它变为ptr_old_stud_name具有 的值ptr_new_stud_name

结果是局部变量ptr_old_stud_name有一个新值。这不会改变它指向的东西,students->name

更具体地说,ptr_old_stud_name是指向 的第一个字符students->namestudents->name是一个数组,不能通过改变指向它的指针来改变它,也不能改变它的地址。要更改其内容,您必须将新值复制到其中的字节中,您可以使用strcpy将字节从new_name.


推荐阅读