I’m teaching myself about structures in C, and am having trouble compiling this code:
#include <stdio.h>
#include <stdlib.h>
struct Date {
int Month;
int Day;
int Year;
};
void AddDecade(struct Date);
int main(int argc, char *argv[]) {
struct Date BDay;
char buffer[50];
printf("What month were you born? ");
BDay.Month = atoi(gets_s(buffer, 50));
printf("What day were you born? ");
BDay.Day = atoi(gets_s(buffer, 50));
printf("What year were you born? ");
BDay.year = atoi(gets_s(buffer, 50));
printf("You were born on %d, %d, %d?\n", BDay.Month, BDay.Day, BDay.Year);
AddDecade(BDay);
printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
}
void AddDecade(struct Date Target) {
Target.Year += 10;
}
The author of the code compiled it without error on a Windows machine, but on my Linux machine gcc gives the following errors:
07_04_structures2.c: In function ‘main’:
07_04_structures2.c:18:5: warning: passing argument 1 of ‘atoi’
makes pointer from integer without a cast [enabled by default]/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but
argument is of type ‘int’07_04_structures2.c:21:5: warning: passing argument 1 of ‘atoi’ makes
pointer from integer without a cast [enabled by default]/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but
argument is of type ‘int’07_04_structures2.c:24:9: error: ‘struct Date’ has no member named
‘year’07_04_structures2.c:24:5: warning: passing argument 1 of ‘atoi’ makes
pointer from integer without a cast [enabled by default]/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but
argument is of type ‘int’
Given this line:
BDay.Month = atoi(gets_s(buffer, 50));
My understanding is that gets_s copies a maximum of 50 bytes of input into buffer, and passes a pointer to the variable ‘buffer’, to atoi, but according to this error:
note: expected ‘const char *’ but argument is of type ‘int’
perhaps gets_s is at fault here? I’ve never used it before…
I’d be very thankful for a detailed explanation of what’s wrong, and how to fix it.
Thanks a lot!
Update:
I implemented all of your recommendations and have come up with the following working code:
#include <stdio.h>
#include <stdlib.h>
struct Date {
int Month;
int Day;
int Year;
};
struct Date AddDecade(struct Date);
int main(int argc, char *argv[]) {
struct Date BDay;
char buffer[50];
char *buffer_end;
printf("What month were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Month = strtol(buffer, &buffer_end, 10);
printf("What day were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Day = strtol(buffer, &buffer_end, 10);
printf("What year were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Year = strtol(buffer, &buffer_end, 10);
printf("You were born on %d, %d, %d?\n", BDay.Month, BDay.Day, BDay.Year);
BDay = AddDecade(BDay);
printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
}
struct Date
AddDecade(struct Date Target) {
Target.Year += 10;
return Target;
}
This has been a great learning experience for me! Thank you! 🙂
get_s()isn’t implemented in the Linux C library, so the compiler assumes the default of it being a function that returns andinttakes whatever (promoted) parameters you pass it.You’ll need to recode to avoid using
gets_s(). Probably the simplest fix is to usefgets()instead. Change the lines similar to:with: