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! :)
BDay.year = atoi(gets_s(buffer, 50));causes the error third line from below, the field is capitalised,Year. - Daniel Fischeratoi. Even though this function is not officially deprecated, it really has no meaningful practical uses, since it provides no feedback in case of an error. If you need to convert a string representation of a number into an actual value, use functions fromstrto...group. Forget aboutato...functions for good. Any time you see it used in the actual code, it is a direct indication of bad code quality. - AnTstrtol,strtoll, &strtoq- Reading the man pages fully illustrate what you mean about atoi being deprecated. Thestrto...functions are really useful because of their support for returning error feedback. From the manual pages: "The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX..." - Totoro