[英]Using pointers with structures to pass data to functions
This is a program for a class project. 这是一个用于班级项目的程序。 It is suppose to be able to create and edit structures.
假定能够创建和编辑结构。 The CreatRec function works fine.
CreatRec函数可以正常工作。 For the ModifyRec I am trying to send it the array by pointers in order to avoid having to "copy" the data.
对于ModifyRec,我试图通过指针将其发送给数组,以避免不得不“复制”数据。 However, I am having trouble getting it to actually change the array.
但是,我很难让它实际更改数组。 ATM The line at the bottom (gr[change].lastname= *info;) is not working at all.
ATM底部的行(gr [change] .lastname = * info;)根本不起作用。 I really have no clue what I am doing wrong here.
我真的不知道我在做什么错。
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
struct student{
int recordname;
char lastname[10];
char firstname[10];
float math;
float english;
float science;
};
//prototypes
int menu();
struct student CreatRec(int);
void ModifyRec(struct student*);
void main()
{
int option, j;//option will be for users menu choice, j makes for loop work for creatrec
struct student grades[10];
j = 0;
option=Menu();
if (option == 1)
for (j = 0; j<10; j++)
(grades[j + 0]) = CreatRec(j);
else if (option==2)
ModifyRec(grades);//dont need & is smart bc array
printf("%s",grades[0].lastname);//This line is checking to see if ModifyRec actaully worked
//free(grades);2
while (1);
}
int Menu()
{
int choi;
printf("Please choose one of the following options.\n 1) Create New Student Records.\n 2) Modify an Existing Student Record\n");
printf(" 3) Print a New Sutdent Record.\n 4) Quit\n");
scanf("%d", &choi);
return choi;
}
struct student CreatRec(int i)
{
struct student qr;
//qr = (struct student*)malloc(sizeof(struct student)*6);
printf("RecordNum %i\n", i);
printf("Please enter last name-->");
scanf("%s", &qr.lastname);
printf("Please enter first name-->");
scanf("%s", &qr.firstname);
printf("Please math grade-->");
scanf("%f", &qr.math);
printf("Please english grade-->");
scanf("%f", &qr.english);
printf("Please science grade-->");
scanf("%f", &qr.science);
return qr;
}
void ModifyRec(struct student gr[])
{
int change;
char feild[10], info[10];
printf("Which record would you like to change?\n");
scanf("%d", &change);
rewind(stdin);
printf("Which feild would you like to edit?\n");
scanf("%s", &feild);
rewind(stdin);
printf("Enter info\n");
scanf("%s", &info);
if (!strcmp("lastname", feild))
gr[change].lastname= *info;//NOT WORKING
}
First of all I do not see a great sense in expression grades[j + 0]
of statement 首先,我认为陈述的表达
grades[j + 0]
没有什么意义
for (j = 0; j<10; j++)
(grades[j + 0]) = CreatRec(j);
These statements 这些陈述
printf("Please enter last name-->");
scanf("%s", &qr.lastname);
printf("Please enter first name-->");
scanf("%s", &qr.firstname);
have to be substituted for 必须代替
printf("Please enter last name-->");
scanf("%s", qr.lastname);
printf("Please enter first name-->");
scanf("%s", qr.firstname);
And this statement 这句话
if (!strcmp("lastname", feild))
gr[change].lastname= *info;//
has to be substituted for 必须代替
if (!strcmp("lastname", feild))
strcpy( gr[change].lastname, info );
gr[change].lastname
is a char array, not a pointer. gr[change].lastname
是一个char数组,而不是一个指针。 You can't reassign it. 您无法重新分配。 In this case, you probably ought to do
scanf("%s", gr[change].lastname);
在这种情况下,您可能应该执行
scanf("%s", gr[change].lastname);
and skip char info[10]
altogether. 并完全跳过
char info[10]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.