Thread: pointer and function argument question.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    pointer and function argument question.

    I am wondering if it is possible to have a function dynamically allocate some memory, as a character array, then return a pointer to that memory to the caller through one or more of the function arguments.

    it may help to show the code I have been playing around with so I'll list it below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int test_func(char *msg)
            {
            int i;
            char *bufptr;
            bufptr = (char *) calloc(100,sizeof(char));
            char recvd[] = "This is a test string";
            for(i=0;i<strlen(recvd);i++)
                    {
                    bufptr[i] = recvd[i];
                    }
            msg = bufptr;
            printf("%s\n%s\n",msg,bufptr);
            return 0;
            }
    
    int main()
            {
            char *msg;
            if(test_func(msg) == 0)
                    {
                    printf("%s\n",msg);
                    }
            free(msg);
            }
    The code compiles and runs on my Linux box, within tes_func the printf is able to successfully print msg and bufptr but in the main function when test_func is called and I try to print the string I had hoped would point to the dynamic string created within the function it segfaults.

    Let me know if there is any way I can clarify this better.

    btw I know the pointer can be returned using return, but I would like to be able to return multiple pointers to dynamically allocated strings with just one function and leave the return values such as -1 -2 -3 for telling the caller at what part of the function the problem occured.

    Thanks

    Steven Fletcher

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Yes, it is possible. But remember that unless you pass a pointer to something, you cannot change its value in the caller (you must pass an int* to change the caller's int, for example). It thus follows that to modify a char*, you must pass a char**:
    Code:
    void f(char **msg)
    {
      *msg = malloc(50);
    }
    int main(void)
    {
      char *msg;
      f(&msg);
    }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    strlen() does not give you the length including the terminating zero, bufptr should be unterminated.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    Thank you.

    Thanks for the help. This makes much more sense now.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Luckily, It'll still work apart from pointer issue, because calloc() zeroed the memory.
    It's better to use standard library function like strcpy/memcpy.
    Last edited by Bayint Naung; 06-14-2010 at 10:23 PM.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Are you referring to the for loop that copies the contents of recvd into bufptr? Or something else?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread