I am trying to passing an array of structures to a function as an argument and change the values of the struct it self. The purpose of the code is to build an agenda. This specific function void newEvent(); should do what it says: adding a new event (new struct). Here is the code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 100
#define N 12
#define HOURS 24
#define MINUTES 60
typedef struct appointment Appointment;
struct appointment {
short int day;
short int month;
short int time[HOURS][MINUTES];
char description[MAX_LENGTH];
};
Appointment meeting[N];
void newEvent (Appointment *meeting, int i, int event) {
short int startingHour = 0, startingMinute = 0, endingHour = 0, endingMinute = 0;
int j, z, flag = 0;
printf("Enter a description of the appointment (max 100 characters): \n");
fgets(meeting[i].description, MAX_LENGTH, stdin);
printf("Date of the appointment (dd/mm): ");
scanf("%hd/%hd", &meeting[i].day, &meeting[i].month);
fflush(stdin);
do {
do {
do {
printf("Starts (hh:mm): ");
scanf("%hd:%hd", &startingHour, &startingMinute);
fflush(stdin);
printf("Ends (hh:mm): ");
scanf("%hd:%hd", &endingHour, &endingMinute);
fflush(stdin);
} while(startingHour > endingHour);
} while((startingHour < 0 || startingHour > 23) || (endingHour < 0 || endingHour > 23) || (startingMinute < 0 || startingMinute > 59) || (endingMinute < 0 || endingMinute > 59));
for (j = startingHour; j <= endingHour; j++) {
for( z = startingMinute; z < MINUTES && flag == 0; z++) {
if (j < endingHour) {
meeting[i].time[j][z] = 1;
}
if ((z > 0) && (z % (MINUTES - 1)) == 0) {
j++;
z = 0;
}
if (j == endingHour && z < endingMinute) {
meeting[i].time[j][z] = 1;
}
if ((j == endingHour) && (z == endingMinute)) {
flag = 1;
}
}
}
flag = 0;
printf("\n");
for(int a = 0; a < event; a++) {
if((meeting[i].day == meeting[a].day) && (meeting[i].month == meeting[a].month)) {
for (j = 0; j < HOURS; j++) {
for( z = 0; z < MINUTES; z++) {
if((meeting[i].time[j][z] == meeting[a].time[j][z]) && meeting[i].time[j][z] == 1) {
flag = 1;
}
}
}
}
}
} while(flag != 0);
event++;
flag = 0;
}
int main() {
int ctrl = 0, event = 0, i, flag = 0, erase = 0;
for(i = 0; i < N; i++) {
for(int j = 0; j < HOURS; j++) {
for(int z = 0; z < MINUTES; z++) {
meeting[i].time[j][z] = 0;
}
}
}
do {
system("cls");
printf("1) Add an appointment\n");
printf("2) Delete an appointment\n");
printf("3) print month appointment\n");
printf("4) Leave\n");
printf("\nYour choice: ");
scanf("%d", &ctrl);
fflush(stdin);
system("cls");
switch (ctrl) {
case 1:
for (i = event; i < (event + 1); i++){
newEvent(meeting, i, event);
}
system("PAUSE");
break;
case 2:
printf("*** DELETING AN EVENT ***\n");
printf("-------------------------\n");
printf("Here down below is your agenda: \n");
flag = 0;
for(int i = 0; i < event; i++) {
printf("Event n. %d description: \n%s\n", i + 1, meeting[i].description);
printf("Date: %hd/%hd\n", meeting[i].day, meeting[i].month);
for(int j = 0; j < HOURS; j++) {
for (int z = 0; z < MINUTES && flag == 0; z++) {
if(meeting[i].time[j][z] == 1) {
printf("Starts: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
for(int j = HOURS; j > 0; j--) {
for (int z = MINUTES; z >= 0 && flag == 0; z--) {
if(meeting[i].time[j][z] == 1) {
printf("Ends: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
printf("-------------------------------\n");
printf("\n\n");
}
printf("Which event you would like to erase? \n");
scanf("%d", &erase);
for (int i = (erase - 1); i < event; i++) {
meeting[i] = meeting[i+1];
event--;
}
flag = 0;
printf("Here down below your updated agenda: \n");
for(int i = 0; i < event; i++) {
printf("Event n. %d description: \n%s\n", i + 1, meeting[i].description);
printf("Date: %hd/%hd\n", meeting[i].day, meeting[i].month);
for(int j = 0; j < HOURS; j++) {
for (int z = 0; z < MINUTES && flag == 0; z++) {
if(meeting[i].time[j][z] == 1) {
printf("Starts: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
for(int j = HOURS; j > 0; j--) {
for (int z = MINUTES; z >= 0 && flag == 0; z--) {
if(meeting[i].time[j][z] == 1) {
printf("Ends: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
printf("-------------------------------\n");
printf("\n\n");
}
system("PAUSE");
break;
case 3:
printf("*** SHOWING ALL YOUR EVENTS ***\n");
printf("-------------------------------\n");
if(event == 0){
printf("Nothing to show here :( \n");
}
flag = 0;
for(int i = 0; i < event; i++) {
printf("Event n. %d description: \n%s\n", i + 1, meeting[i].description);
printf("Date: %hd/%hd\n", meeting[i].day, meeting[i].month);
for(int j = 0; j < HOURS; j++) {
for (int z = 0; z < MINUTES && flag == 0; z++) {
if(meeting[i].time[j][z] == 1) {
printf("Starts: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
for(int j = HOURS; j > 0; j--) {
for (int z = MINUTES; z >= 0 && flag == 0; z--) {
if(meeting[i].time[j][z] == 1) {
printf("Ends: %d:%d\n", j, z);
flag = 1;
}
}
}
flag = 0;
printf("-------------------------------\n");
printf("\n\n");
}
system("PAUSE");
break;
}
} while(ctrl != 4);
return 0;
}
Unfortunately I got a warning which says warning: passing argument 1 of 'newEvent' from incompatible pointer type [-Wincompatible-pointer-types]
PS: The code i've putted here is a little bit simplified (you can see it from ...), but I think is enough to let people way better then me at coding in C understand where the problem is.
newEventonly needs to work with one meeting, and it is given the space (an existing structure, which can be an element of an array of structures) for that meeting, then just make its parameterAppointment *meeting. The expression&meeting[i]is the address of a single structure,meeting[i], so it is anAppointment *. You do not need**unless you want to pass not just the address of a thing but an address of a pointer to a thing.Appointment meeting[N];,meetingis an identifier that designates the array. It is not a pointer to the array or a pointer to the first element of the array.&meetingis not the address of a pointer; it is the address of the array. When used in expressions other than as the operand ofsizeofor unary&,meetingwill be converted to a pointer to its first element, but it is not itself such a pointer. Passing&meetingdoes not pass anAppointment **.meetingis an array of structures.meeting[i]is one structure.&meeting[i]is the address of one structure. To changemeeting[i]. allnewEventneeds is&meeting[i]. It does not need bothmeetingandi.&meeting[i]to the function there is no way to refer to each struct of the array inside the function