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:\n");
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\n", studentID, year, month, day, phone);
}
}
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
To see the task it is better at least for the first time to write some block-scheme of what you have to do in a program. In your case:
- Read data from user (each structure).
- Increase array size, add new structure.
- Loop 1-2 until input user finish adding new people (needs some condition here to finish).
- Find necessary structure and print it.
So the first step is to read information from user. You can use scanf():
In the simplest way you can do that step-by-step for each field:
#include <stdio.h>
...
int value;
scanf("%d", &value);
...
In case of success this function should return number of items it reads (1 in your case).
For phone you should use scanf("%ld", &phone).
To resize array use function realloc() (#include :
realloc(&ptr_to_array, new_size);
Each elements of the array is a pointer to structure "student".
Next steps are similar.
The first problem is that you have variable names the same as the name of the type. Although you can have that in C to a certain extent, like:
typedef int x;
x foo (x x)
{
return x;
}
It might be a good idea not to do this for the readability purposes. So in your case you have:
int * studentData= NULL;
int * studentDataType= NULL;
which is a variable name, then you have:
struct studentDataType ...
which is a name of the type (should be used as struct studentDataType, not without struct as you do); finally
studentDataType *studentData = ...
is treated by the compiler as an operation on two variables, not a type declaration as you would expect. So your memory allocation needs to be:
struct studentDataType *studentData = malloc(numberOfStudents *sizeof(struct studentData));
Which brings a problem that you redefine studentData, which you declared in the beginning of the program, and "numberOfStudents" is not defined, probably you wanted to write "students" instead.
As for the reading data with scanf, see the previous comment.