You are not logged in.

#1 2009-03-30 15:36:38

sa
Member
From: boston ma
Registered: 2008-05-23
Posts: 127
Website

probably very simple C error

i've got this bit of code that is causing my program to end early. it must be a stupid mistake i'm overlooking

char name;

printf("Welcome!\n\n");
printf("Please enter your name: ");
scanf("%s", &name);
fflush(stdin);

printf("\n\nHello %s", name);

but when I execute the program, this is my output:

Welcome!

Please enter your name:

and then you can enter text, but the program terminates immediately after.

Offline

#2 2009-03-30 15:56:55

meqif
Member
From: Portugal
Registered: 2006-12-16
Posts: 60
Website

Re: probably very simple C error

Is that a buffer overflow I smell? tongue

You're saving the user's input to a single char, resulting in an overflow, followed by a segfault.

You should either allocate a buffer with malloc or create a char array with an appropriate size. Beware that you'll be vulnerable to a buffer overflow anyway. To avoid that, specify a maximum "width"; that is, a maximum number of characters to be read.

EDIT: Here's how you'd fix it:

char name[80]; /* 79 chars + 1 literal 0 (aka '\0', aka "end of string") */

...

scanf("%79s", &name); /* Read at most 79 chars */

Last edited by meqif (2009-03-30 16:05:39)


Ricardo Martins ><>< ricardomartins.cc ><>< GPG key: 0x1308F1B4

Offline

#3 2009-03-30 15:58:44

Shunpike
Member
From: France
Registered: 2009-01-28
Posts: 47

Re: probably very simple C error

char name;

Sounds like you're trying tu put a string in a single char. Bad idea wink

Offline

#4 2009-03-30 16:22:41

sirius
Member
From: Norway
Registered: 2008-12-25
Posts: 68

Re: probably very simple C error

char name[80+1];

is a nice way to "remember" to add space for the null character :-)

Offline

#5 2009-03-30 17:10:09

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: probably very simple C error

and btw, fflush(stdin) is undefined(that is, on some compiler/platform combination it works as you expect, it flushes the stdin, but not in linux

this code should flush the stdin in any platform:

void flush_in(void)
{
   int ch;

   while( (ch = fgetc(stdin)) != EOF && ch != '\n' ){}
}

-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'

Offline

#6 2009-03-30 18:19:56

Napaim
Member
From: Scotland
Registered: 2009-03-09
Posts: 26

Re: probably very simple C error

Not that good with C but wouldn't it be best using pointers to do this?

    char *name;
    scanf("%s", &name);
    printf("%s\n", &name);

Something like that?

Offline

#7 2009-03-30 18:28:37

meqif
Member
From: Portugal
Registered: 2006-12-16
Posts: 60
Website

Re: probably very simple C error

Napaim wrote:

Not that good with C but wouldn't it be best using pointers to do this?

    char *name;
    scanf("%s", &name);
    printf("%s\n", &name);

Something like that?

You'd still need to allocate some memory with malloc or similar (unless you use 'a' in the format, which will allocate a buffer automatically, but that's a GNU extension, and thus not portable). And you'd better define some limit to the characters you'll read at most, otherwise you risk a buffer overflow.

Seriously guys, user input is potentially deadly to your apps. Code like everyone using your programs is either really dumb or really evil.

Last edited by meqif (2009-03-30 18:31:36)


Ricardo Martins ><>< ricardomartins.cc ><>< GPG key: 0x1308F1B4

Offline

#8 2009-03-30 18:30:51

Arkane
Member
From: Switzerland
Registered: 2008-02-18
Posts: 263

Re: probably very simple C error

Napaim wrote:

Not that good with C but wouldn't it be best using pointers to do this?

    char *name;
    scanf("%s", &name);
    printf("%s\n", &name);

Something like that?

scanf doesn't allocate memory for its return arguments, does it? You need an explicit malloc in this example.

Last edited by Arkane (2009-03-30 18:31:16)


What does not kill you will hurt a lot.

Offline

#9 2009-03-30 19:51:52

ArchGh0ul
Member
Registered: 2008-10-23
Posts: 96

Re: probably very simple C error

#include <stdio.h>
int main(int argc, char *argv[])
{       
        char name[64];
        printf ("What shall ye be called as? : ");
        scanf ("%63%s", name);
        printf("Ohai %s o/\n", name);
return 0;
}

Last edited by ArchGh0ul (2009-03-30 19:55:57)

Offline

#10 2009-03-31 00:14:20

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: probably very simple C error

.

Last edited by fumbles (2020-09-26 11:50:12)

Offline

#11 2009-03-31 00:22:51

sa
Member
From: boston ma
Registered: 2008-05-23
Posts: 127
Website

Re: probably very simple C error

thanks to all of you!

Offline

#12 2009-03-31 06:38:51

ArchGh0ul
Member
Registered: 2008-10-23
Posts: 96

Re: probably very simple C error

fumbles wrote:
ArchGh0ul wrote:
#include <stdio.h>
int main(int argc, char *argv[])
{       
        char name[64];
        printf ("What shall ye be called as? : ");
        scanf ("%63%s", name);
        printf("Ohai %s o/\n", name);
return 0;
}

You need a & when you using scanf.
As in:

scanf("%s", &name);

Wrong. Strings defined as arrays (like in my example) don't require the reference character because it's already a pointer (char name[] = char *). Strings are a particular type in this case
Since scanf expects a pointer you need the & only when referencing a normal variable (eg "int bla;..."),not when you already pass a pointer ("char string[x]")

PS
Also since when are warnings errors? There are actually warnings that sometimes you CAN just ignore... No one is forcing you to use -Wall. My code with all those compilation flags will output ONE warning which ain't that important anyway

Last edited by ArchGh0ul (2009-03-31 07:00:57)

Offline

#13 2009-03-31 11:24:45

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: probably very simple C error

.

Last edited by fumbles (2020-09-26 11:49:09)

Offline

Board footer

Powered by FluxBB