Program crashes

This is a discussion on Program crashes within the C Programming forums, part of the General Programming Boards category; Can someone help me check the attached file, it is a simple program, but the program crashes after a scanf ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Program crashes

    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...
    Attached Files Attached Files
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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?
    Only by the cross are you saved...

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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!
    Only by the cross are you saved...

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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...
    Only by the cross are you saved...

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    did u get the same scanf problem?
    Only by the cross are you saved...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post your latest code attempt if you're still having problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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...
    Attached Files Attached Files
    Only by the cross are you saved...

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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?
    Only by the cross are you saved...

  10. #10
    ___
    Join Date
    Jun 2003
    Posts
    806
    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.)
    I just messed around and compiled it and got these errors.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    try replacing the line #define END NULL with #define END '\0'
    Only by the cross are you saved...

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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?
    Only by the cross are you saved...

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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()?
    Only by the cross are you saved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 05:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 05:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2002, 12:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21