Thread: Getting error while trying to print out a char pointer

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Getting error while trying to print out a char pointer

    I keep getting run failed when ever i try to print out this character.
    Code:
      char *c;
      *c='9';
       
        printf ("%c",*c);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code exhibits undefined behaviour, as c has not been initialised so does not point at anything. It is necessary to ensure c points at something valid, before dereferencing *c (even accessing the value of *c gives undefined behaviour, let alone assigning a value to *c as you are)

    Code:
       char x;
       char *c = &x;
    
       *c = '9';   /*  this will change x  */
    
       printf ("%c",*c);
    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. Char Pointer error!
    By brack in forum C++ Programming
    Replies: 11
    Last Post: 10-17-2010, 01:59 PM
  2. Memory error when copying char pointer
    By sismail in forum C Programming
    Replies: 8
    Last Post: 03-08-2010, 12:00 PM
  3. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  4. char pointer error
    By Marksman in forum C Programming
    Replies: 8
    Last Post: 02-26-2005, 06:57 PM
  5. print magnified char
    By vasu15 in forum C Programming
    Replies: 2
    Last Post: 09-01-2003, 07:21 AM