PDA

View Full Version : [Solved] gets_s() ?


lbl
January 18th, 2010, 04:59
Hi

Im following a video explaning C but now im stumbling across an example that wont compile.

The dude from VTC makes it very clear that its the C90 standart im looking at but it dosent seem so ...

This is the example:

#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];

printf("what month where you born? ");
Bday.Month = atoi(gets_s(buffer, 50));

printf("What day where you born? ");
Bday.Day = atoi(gets_s(buffer, 50));

printf("What year where you born? ");
Bday.Year = atoi(gets_s(buffer, 50));

printf("You where 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;
}

And this is the compile error:

[lbl@atom0 ~/c-test-code]$ cc struct2.c
struct2.c: In function 'main':
struct2.c:19: warning: passing argument 1 of 'atoi' makes pointer from integer without a cast
struct2.c:22: warning: passing argument 1 of 'atoi' makes pointer from integer without a cast
struct2.c:25: warning: passing argument 1 of 'atoi' makes pointer from integer without a cast
/var/tmp//ccsiuwYr.o(.text+0x2d): In function `main':
: undefined reference to `gets_s'
/var/tmp//ccsiuwYr.o(.text+0x5c): In function `main':
: undefined reference to `gets_s'
/var/tmp//ccsiuwYr.o(.text+0x8b): In function `main':
: undefined reference to `gets_s'
[lbl@atom0 ~/c-test-code]$

Anyone got any clues ... i have been trying different stuff with gets and i can get that to work but "cc" keeps telling me that its not a secure way due to the fact that it can overflow.

/lbl

aragon
January 18th, 2010, 06:01
It's not part of C90. It came after C99.

https://buildsecurityin.us-cert.gov/daisy/bsi/articles/knowledge/coding/300-BSI.html

Use fgets().

lbl
January 18th, 2010, 06:07
I guess thats why ...

Bday.Month = atoi(fgets(buffer, 50, stdin));

This works great thanks.

/lbl

ta0kira
January 23rd, 2010, 02:46
While you're at it, you might as well throw out atoi and replace it with strtol since atoi is deprecated.
Kevin Barry