rsorce
rsorce

Reputation: 1

How to assign a value to a struct that is returned through a function in C

 extern int ID; // student ID number
    struct personalDetails det; // pD
    struct classRecords rec; // cR
    struct student st1; // student 1
    struct student st2; // student 2
    struct student st3; // student 3
    struct student st4; // student 4
    struct student st5; // student 5

    char pD[30]; // for storing values that will be assigned to personalDetails
    double cR; // for storing values that will be assigned to classRecords

    struct student studentID(int id) { // identifies student by ID
    if (id == 1) {
            struct student x = st1;
            return x;
    }
    else if (id == 2) {
            struct student x = st2;
            return x;
    }
    else if (id == 3) {
            struct student x = st3;
            return x;
    }
    else if (id == 4) {
            struct student x = st4;
            return x;
    }
    else if (id == 5) {
            struct student x = st5;
            return x;
    }
    }

I need the function updateName() to assign a value to 'name' which is a value within another struct personalDetails.

 void updateName() {
    printf("Enter student's name\n");
    scanf("%s", pD);

    studentID(ID).det.name = pD;

I'm currently getting this error:

operations.c: In function ‘updateName’:
    operations.c:55:24: error: lvalue required as left operand of assignment
     studentID(ID).det.name = pD;

Please tell me how I can fix this, thanks in advance!

EDIT 1: Here's the struct defs for those interested.

struct personalDetails {
        char *name;
        char *phoneNum;
        char *address;
};

struct classRecords {
        double assignment;
        double midterm;
        double finalMark;
        double total;
};

struct student{
        struct personalDetails det;
        struct classRecords rec;
};

Thanks for the answers, strcpy got rid of the compilation error, but now when I enter a value for 'name' I get a 'segmentation fault'. Anyone know why?

Upvotes: 0

Views: 84

Answers (4)

chqrlie
chqrlie

Reputation: 144750

studentID(ID) returns a copy of the structure where you store the data, that you need to store into another structure. You probably want to return a pointer to it and use this syntax:

strcpy(studentID(ID)->det.name, pD);

But you should also pay attention to potential buffer overflows: for example, scanf with a %s format does not know the size of the array pointed to by pD. You should write this instead:

scanf("%29s", pD);

But telling scanf about buffer sizes is very cumbersome as the size information must be hard coded in the format string and may become out of sync if you later change the actual size of the array pD. scanf is very tricky to use correctly.

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

I guess you want something like this. Use pointers.

extern int ID; // student ID number
struct personalDetails det; // pD
struct classRecords rec; // cR
struct student st1; // student 1
struct student st2; // student 2
struct student st3; // student 3
struct student st4; // student 4
struct student st5; // student 5

    char pD[30]; // for storing values that will be assigned to personalDetails
    double cR; // for storing values that will be assigned to classRecords

struct student *studentID(int id) { // identifies student by ID
    if (id == 1) {
            return &st1;
    }
    else if (id == 2) {
            return &st2;
    }
    else if (id == 3) {
            return &st3;
    }
    else if (id == 4) {
            return &st4;
    }
    else if (id == 5) {
            return &st5;
    }
}

 void updateName() {
    struce student *s;
    printf("Enter student's name\n");
    scanf("%29s", pD);

    s = studentID(ID);
    s->det.name = malloc(strlen(pD) + 1);
    strcpy(s->det.name, pD);
}

I'm not sure if this is correct because I don't know the definition of struct student, but please don't do such a foolish thing as assigning a (pointer to the first element of) fixed array to multiple structs.

Upvotes: 0

tdao
tdao

Reputation: 17668

You haven't shown your struct student declaration; but looks like name is a character array. Then,

scanf("%s", &pD); is incorrect, should be scanf("%s", pD); as pD is already the address of the string character.

Also, studentID(ID).det.name = pD; is incorrect as well, you can't use direct assignment for string variable, use strcpy instead.

Upvotes: 0

Jake Psimos
Jake Psimos

Reputation: 640

C does not allow you to directly assign strings using the equal operator. You can use strncpy (include string.h). Read about the function by typing 'man strncpy' in the terminal or googling it.

Upvotes: 0

Related Questions