Can someone help me check the attached file, it is a simple program, but the program crashes after a scanf statement. I can't figure out why...
Printable View
Can someone help me check the attached file, it is a simple program, but the program crashes after a scanf statement. I can't figure out why...
the program does not seem to crash when i change the following declaration :
char *temp;
to
char temp[50]; etc.
why is this so?
also, if u try compiling and find some warning messages, can anybody tell me how do i change some line of code which is causing this warning?
I've not looked at your source file, I don't think I need to!
char *temp defines a pointer to something, but does not allocate space to store that something. You'll need to dynamically allocate storage space if you want to do it this way, use malloc() and remember to free() the storage after your finished with it.
char temp[50] allocates the space.
oh man yeah! after thinking twice, i've suddenly woke up
wat am i thinking
wat made me think this way is this sample i got from msdn on atoi
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: \"%s\"; integer: %d\n", s, i );
here, they define s as char *s;
how come they assign "-9885 pigs" to s? hehe...thanx a lot!
i see
thanx
i still have a problem
when entering input, the program forces me to enter the input a few times before it actually continues executing the program...
did u get the same scanf problem?
Post your latest code attempt if you're still having problems.
here it is, problem with scanf statement during runtime, i seem to have to enter the input for a few times before it continues executing the subsequent lines of code...
ok, but wat if i wan getchar() for the part where i enter characters, becoz i want the characters entered one by one...like this :
printf ("Please enter the number of characters you would like to enter : ");
fgets( temp, sizeof(temp), stdin );
length = atoi(temp);
printf("\nPlease enter %d characters : ", length);
for (i=1;i<=length;i++){
printf ("Character %d :", i);
character = getchar();
j = i+1;
current->next = insertAtEnd(character, j);
current = current->next;
printf("\n");
}
somehow there is still a problem when entering characters, seems like there is still a '\n' in the buffer which causes it to skip entering characters...anyway aroun this?
I just messed around and compiled it and got these errors.Code:c:\docume~1\justin\locals~1\tempor~1\content.ie5\x08f51op\homewo~1.c: In function `insertAtEnd':
c:\docume~1\justin\locals~1\tempor~1\content.ie5\x08f51op\homewo~1.c:54: `END' undeclared (first use in this function)
c:\docume~1\justin\locals~1\tempor~1\content.ie5\x08f51op\homewo~1.c:54: (Each undeclared identifier is reported only once
c:\docume~1\justin\locals~1\tempor~1\content.ie5\x08f51op\homewo~1.c:54: for each function it appears in.)
try replacing the line #define END NULL with #define END '\0'
i see, ok, so i have no way but to use fgets...
but can fgets be coded in a way to simulate wat happens in the getchar() function?
like press any key and it immediately executes the next line of code?
becoz if i'm not mistakened, fgets just holds in a buffer watever ht euser enters rite?
i see, so i have no choice but to use fgets ah?
so it is up for me to trust the user to enter just 1 character into the buffer and press enter if i want to simulate a getchar()?