You are not logged in.
Pages: 1
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
Is that a buffer overflow I smell?
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
char name;
Sounds like you're trying tu put a string in a single char. Bad idea
Offline
char name[80+1];
is a nice way to "remember" to add space for the null character :-)
Offline
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
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
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
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
#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
.
Last edited by fumbles (2020-09-26 11:50:12)
Offline
thanks to all of you!
Offline
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
.
Last edited by fumbles (2020-09-26 11:49:09)
Offline
Pages: 1