Thread: Assigning a char value to char pointer doesn't work.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    6

    Assigning a char value to char pointer doesn't work.

    I'm trying to assign a char value to an char pointer. The problem is
    when I comment
    Code:
    char *a = "Some Birds Can't Fly";
    the printed display is
    Code:
    B
    however without commenting above, it gives, lot of scrambled characters. Can you check the code and let me know what's happening.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *assign_char(char *str, char ch)
    {
    
    
        size_t len = strlen(str);
        char *strRslt = malloc( len + 2 );
    
        strcpy(strRslt, str);
        strRslt[len] = ch;
        strRslt[len+1] = '\0';
        
        return strRslt;
    
    }
    
    int main(void)
    {
        char *b;
        char *a = "Some Birds Can't Fly";
        char ch = 'B';
    
        b = assign_char(b, ch);
    
        printf("%s\n", b);
    
        return 0;
    }

    compiled with
    Code:
    gcc main.c -o main

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    b = assign_char(b, ch);
    it should have been:
    Code:
    b = assign_char(a, ch);
    Incidentally, to avoid potential attempts to change a string literal, your code should be more const-correct, e.g., in main:
    Code:
    const char *a = "Some Birds Can't Fly";
    and for the assign_char function:
    Code:
    char *assign_char(const char *str, char ch)
    It might be more accurate to rename assign_char to append_char.

    Also, remember to free() what you malloc(). In assign_char, you should also check that malloc did not return a null pointer before you use what it returns.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    6

    Thanks for your prompt reply.

    Quote Originally Posted by laserlight View Post
    This is wrong:
    Code:
    b = assign_char(b, ch);
    it should have been:
    Code:
    b = assign_char(a, ch);
    My need is to append the ch with b, a is for demo my problem when I'm having a "a" kind of variable. Hope you get it.So isn't that correct to pass the b = assign_char(b, ch);

    Quote Originally Posted by laserlight View Post
    Incidentally, to avoid potential attempts to change a string literal, your code should be more const-correct, e.g., in main:
    Code:
    const char *a = "Some Birds Can't Fly";
    and for the assign_char function:
    Code:
    char *assign_char(const char *str, char ch)
    I tried this. However my result is same, see the attached image below, it shows what I'm getting.
    Assigning a char value to char pointer doesn't work.-selection_037-png

    Quote Originally Posted by laserlight View Post
    It might be more accurate to rename assign_char to append_char.

    Also, remember to free() what you malloc(). In assign_char, you should also check that malloc did not return a null pointer before you use what it returns.
    Thanks a lot for the tip, I agree with you, these should definitely be implemented.

    Back again to my original question, why when an unused string variable is there (a), the out put gets so wired. when I comment that "char *a = "Some Birds Can't Fly";" line. things are normal.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because if you start with a pointer in main() that is uninitialised, when you get to
    strcpy(strRslt, str);
    you're copying garbage.

    It's really that simple.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    6

    Thats Really making Sense after all.

    Quote Originally Posted by Salem View Post
    Because if you start with a pointer in main() that is uninitialised, when you get to
    strcpy(strRslt, str);
    you're copying garbage.

    It's really that simple.
    That's really impressive, thanks for pointing me out. I never thought that way.
    Can you please explain me so if I have some bunch of initialized variables after main() like "int x = 1;" will my problem solved?

    Actually I tried it just now, still getting the scrabbled print.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can you please explain me so if I have some bunch of initialized variables after main() like "int x = 1;" will my problem solved?
    No it won't.

    What you're observing is undefined behaviour.
    Your initial observation is down to pure dumb luck. Your 'uninitialised' pointer just happened to point to an empty string instead of a bunch of garbage.

    Remember, C is a language with very few safety nets.
    If you ask "I want to hang myself", C will happily give you the rope to do it.
    Other languages will either ask "are you sure?" or simply say "no".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning value to char pointer
    By Ducky in forum C Programming
    Replies: 3
    Last Post: 02-03-2013, 07:26 AM
  2. Replies: 3
    Last Post: 06-18-2012, 01:06 PM
  3. char* doesn't work as intended
    By Queue in forum C Programming
    Replies: 10
    Last Post: 09-10-2006, 05:05 AM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. Scanf for char doesn't work in loop
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 10-18-2001, 04:42 AM

Tags for this Thread