Thread: Char pointer passed to function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    27

    Char pointer passed to function

    Code:
    #include  <stdio.h>
    
    int main(void)
    {
        // you need malloc otherwise memory error.
        char* string=(char*)malloc(5);
        set(string);
        printf("%s",string);
        return 0;
    }
    
    void set(char* s) {
        s="abc";
    }
    This has no output. Help.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    This works.

    Code:
    #include  <stdio.h>
    
    int main(void)
    {
        // you need malloc otherwise memory error.
        char* string=(char*)malloc(5);
        set(string);
        printf("%s",string);
        return 0;
    }
    
    void set(char* s) {
        *s='a';
        *(s+1)='b';
        *(s+2)='c';
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the first code example, you are replacing the value of the local pointer with another value, so of course it doesn't affect the value of the pointer in main, or the value of what that pointer points to. In the second code example, you are modifying what the local pointer points to, and this is the same as what the pointer in the caller points to, hence the changes are reflected in main.
    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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    Quote Originally Posted by laserlight View Post
    In the first code example, you are replacing the value of the local pointer with another value, so of course it doesn't affect the value of the pointer in main, or the value of what that pointer points to. In the second code example, you are modifying what the local pointer points to, and this is the same as what the pointer in the caller points to, hence the changes are reflected in main.
    Thx for the clarification. The following works now

    Code:
    #include  <stdio.h>
     
    int main(void)
    {
        // you need malloc otherwise memory error.
        char* string=(char*)malloc(5);
        set(&string);
        printf("%s",string);
        return 0;
    }
     
    void set(char** s) {
        *s="abc";
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gunitinug
    The following works now
    While it may "work" in the sense of "compile and run without crashing, printing expected output", it isn't correct:
    • Before you call a function, it should be declared, either by placing its definition before the definition of the function in which it is called, or by forward declaring it.
    • To use malloc, you should #include <stdlib.h>
    • Whenever you use malloc, you should have a corresponding call to free.
    • You don't need malloc at all: you are going to overwrite the pointer with a pointer to the first character of "abc", so you don't need to allocate any additional space for the pointer to point to.
    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

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Please do not use cast for malloc function.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    And further to laserlight's comment, if you did call free, it would most likely crash.
    Code:
    char* string=malloc(5);
    set(&string);
    printf("%s",string);
    free(string); //!! oops, string no longer points to what malloc returned
    return 0;
    You were on the right track in post #2, where this is simpler.
    Code:
    void set(char* s) {
        strcpy(s,"abc");
    }
    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. Replies: 2
    Last Post: 12-02-2012, 05:25 AM
  2. passed by reference or pointer ?
    By vohu in forum C Programming
    Replies: 15
    Last Post: 02-23-2012, 12:39 PM
  3. string pointer passed to a function help
    By BrandNew in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 11:08 AM
  4. help undestanding what pointer is being passed
    By reakinator in forum C Programming
    Replies: 1
    Last Post: 05-10-2008, 12:06 AM
  5. Manipulating a passed pointer inside a function.
    By see_c in forum C Programming
    Replies: 12
    Last Post: 05-25-2006, 02:35 PM

Tags for this Thread