#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11

    hrM, newb problem, pass in char*, inside func malloc


    Code:
    void xx(char *gG)
    {
    	gG=(char*)malloc(100*sizeof(char)); 
    	strcpy(gG,"B E L K I N"); 
    	return; 
    }
    
    char *GG=NULL; 
    xx(&GG); 
    printf("* %s\n", GG); 
    free(GG);
    How would I get this to work? right now I have it return a char*, which works, but what's wrong witht he above.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    In the function, gG is a pointer. Within the function, you change its value, but since gG is passed in as a value, that change never gets out of the function exactly as it you had passed a int argument.

    In the C solution, you need to pass in a pointer to that pointer; the C++ solution would allow you to use a reference instead. Here is how I would rewrite your example:
    Code:
    void xx(char **gG)
    {
    	*gG=(char*)malloc(100*sizeof(char)); 
    	strcpy(*gG,"B E L K I N"); 
    	return; 
    }
    
    char *GG=NULL; 
    xx(&GG); 
    printf("* %s\n", GG); 
    free(GG);
    Now, sometimes pointers to pointers can cause the notation to get a bit messy within a function. I recently had the experience of my function not working until I resorted to initializing local pointer variables which I then assigned to the arguments before exiting. So the following function body may be preferable for readability:
    Code:
    void xx(char **gG)
    {
    	char *cp;  /* temp local pointer variable */
    
    	/* do the allocation and initialization with the local pointer */
    	cp=(char*)malloc(100*sizeof(char)); 
    	strcpy(cp,"B E L K I N"); 
    
    	/* now pass the local pointer's value to the argument */
    	*gG=cp; 
    	return; 
    }
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I am not sure why what you are trying to do doesn't work, but this does:

    Code:
    char * xx(){
        char * gG=(char*)malloc(100*sizeof(char)); 
        strcpy(gG,"B E L K I N"); 
        return gG; 
    }
    
    int main(){
        char *GG=NULL; 
        GG = xx(); 
        printf("* %s\n", GG); 
        free(GG); 
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    xx() takes a char* parameter, but you are passing &GG which has type char**.

    What compiler are you using? I would have expected the compiler to have at least issued warning for this. If it did not, check your compiler's options for maximizing the level of warnings.

    My advice, always use the maximum warning level, and always treat warnings as errors. The quality of the code will be better and more portable.

    BTW I prefer mitakeet's solution. Returning a value via function return value is always prefereable to returning via a pointer parameter.

    Clifford.
  8. #5
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    I am using mitakeet's method at the moment, I was just wondering what was wrong with the other meth0d.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    The proposed solutions showed you what works rather than what was wrong with your code, which is what you asked.

    I was trying to take a broader view, and I was more concerned that your compiler was not generating warnings (or that you were ignoring them), since this is more fundamental and will bite you in many other ways in the future.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo