user3486669
user3486669

Reputation: 11

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. For the ModifyRec I am trying to send it the array by pointers in order to avoid having to "copy" the data. 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. 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

}

Upvotes: 1

Views: 264

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

First of all I do not see a great sense in expression grades[j + 0] of statement

    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 );

Upvotes: 1

guest
guest

Reputation: 6698

gr[change].lastname is a char array, not a pointer. You can't reassign it. In this case, you probably ought to do scanf("%s", gr[change].lastname); and skip char info[10] altogether.

Upvotes: 0

Related Questions