Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

c - How do I use a structure?

Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).

Here is my code, I've gotten some suggestions and even bits of code from you guys, but after implementing them I'm even more confused on what I should do. Please take a look at it and critique me.

EDIT: I also added in on the code where I'm receiving and error

Thank you.

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int * studentData= NULL;
    int * studentDataType=NULL;
    int students;
    int studentID;
    int year;
    int month;
    int day;
    long long phone;

    printf("How many students are you entering records for:
");
    scanf("%d", &students);

    studentData=(int*)malloc((sizeof(int)*students));

    struct studentDataType
    {
        int studentID; 
        int year;
        int month;
        int day;
        long long phone;
    };
    //invalid operands to binary * (have 'int' and 'int *')
    studentDataType *studentData = (studentDataType*)malloc(numberOfStudents *sizeof(studentData));

    for (int i = 0 ; i < students ; ++i) 
    {
        printf("%d, %d, %d, %d, %d
", studentID, year, month, day, phone);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're redefining studentData

int * studentData= NULL;

then later

studentDataType *studentData = (studentDataType*)malloc(numberOfStudents * sizeof(studentData));

You should declare the studentDataType struct first (outside of main() ) then use it in your original declaration


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...