Thread: Simple C Program that sometimes crashes!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Question Simple C Program that sometimes crashes!

    Hello everyone, I am having trouble understanding why "12345" is not being displayed and instead the program either displays nothing or it will crash. Helps!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char* ch =0;  /* A nice charstar to hold 12345 as a string */
        int num=12345; /*12345 as a numerical value rather than a string */
        sprintf(ch,"%i",num); /*To convert the integer num into a string and store in it charstar! */
        printf("%s\n",ch); /*I want to see the results */
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You have initialized ch as the NULL pointer. Writing any data to it (which is what you are doing with sprintf()) gives undefined behaviour.

    You need to make it point at a buffer long enough to handle the content you are writing to it.

    Code:
        char ch[6];
    will do the trick (assuming you don't try to write more than a 5-digit number to it: sprintf() adds a trailing zero terminator).

    More generally: a pointer is not an array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Thanks for your help, so what you are saying is, I must define the size of the string I want to store in the char* variable before I actually try to write anything to it and never set char* to 0 aka NULL.
    Last edited by logitechz; 12-03-2011 at 03:45 PM.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yes, that's right.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by TheBigH View Post
    Yes, that's right.
    Only in the very loosest and most imprecise sense.

    Have a look at this link for one guy's attempt to explain the differences between pointers and arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes at very end
    By Kudose in forum C++ Programming
    Replies: 15
    Last Post: 07-23-2009, 04:06 AM
  2. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  3. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 01:16 AM
  4. Learning C++ - Coded simple program which crashes
    By vexir in forum C++ Programming
    Replies: 6
    Last Post: 09-09-2004, 11:23 AM
  5. Program crashes
    By fkheng in forum C Programming
    Replies: 12
    Last Post: 06-24-2003, 04:59 AM