c - 用指针交换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);
}
}
解决方案
Replace_Strings(&ptr_old_stud_name,&ptr_new_stud_name);
ptr_old_stud_name
将和的地址传递ptr_new_stud_name
给ReplaceStrings
。
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->name
。students->name
是一个数组,不能通过改变指向它的指针来改变它,也不能改变它的地址。要更改其内容,您必须将新值复制到其中的字节中,您可以使用strcpy
将字节从new_name
.
推荐阅读
- reactjs - Redux -> 如何在一个存储中保存多个对象?
- azure - 如何使用 Azure 大规模安排作业?
- php - 在多维数组中不包含给定值的未设置数组
- flutter - Image.network 停止显示图像
- java - 我无法在 java(Intelli j) 中连接 mysql 数据库
- sql - PostgreSQL SQL查询以查找字符串中子字符串的出现次数
- html - 将鼠标悬停在除选定文本之外的所有内容上
- angular - ng-select:使用 ng-option 和 ng-template 有什么区别?
- php - 为什么没有分隔记录的json文件(PHP)
- c# - 如何将 localhost wcf 服务与 asp.net 4.0 连接