Thread: Cannot convert char* to char**

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Cannot convert char* to char**

    Hello,

    I am seem to be getting this error message. I know that ** is pointer to pointer. However, I am not too familier with that.

    This is what I am doing. I have an API function that takes the following parameters.

    Code:
    GetErrorDescription (IN RESULT in_Result, OUT CHAR ** outDescription );
    
    char *error_msg = (char*) malloc(sizeof *error_msg);
    GetErrorDescription(result, error_msg);
    Regards,

    Steve

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    can you show the exact error you are getting?

    To convert a char* to a char** do this -

    Code:
     
    char* pChar; // pChar is a char*
     
    foo(&pChar);  // &pChar is a char**

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer to pointer is simply a ... pointer. So char** would be a pointer to a... that's right, char* variable.
    Sounds sensible?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, I suspect, you should not allocate 1 char to that pointer - either you allocate enought o hold an error message [a single char can only hold one stringe value - the empty string, since one char, to be a valid string, can only be the zero-value used for the termination].

    However, the common reason you pass a string into a function is so that the calling function can assign a pointer with the value of (for example) the memory allocated for the string. If that is the case, whatever memory the pointer to char contained before you passed it in would be completely lost (so you have a memory leak).

    Since there are several "GetErrorDescription", I don't know which one you are actually using, and what it expects in the form of the char ** argument passed in.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    perhaps something like
    Code:
    char **error_msg;
    error_msg = (char**) malloc(sizeof *error_msg);

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    perhaps something like
    Code:
    char **error_msg;
    error_msg = (char**) malloc(sizeof *error_msg);
    Probably not [and since this is C, you definitely shouldn't cast the result of malloc]. Why would you want to allocate 4 bytes (or 8, or ver rarely 2 bytes), when a local variable is a fine way to solve this, as posted by abachler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by matsp View Post
    Probably not [and since this is C, you definitely shouldn't cast the result of malloc]. Why would you want to allocate 4 bytes (or 8, or ver rarely 2 bytes), when a local variable is a fine way to solve this, as posted by abachler.

    --
    Mats
    That would depend on what use the OP wants to put it to.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    That would depend on what use the OP wants to put it to.
    And in what use-case is your suggestion better than the one suggested by abachler?

    And don't say "if the user want's to return the pointer", because that is really a BAD case of not passing it in from the level below.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by matsp View Post
    And in what use-case is your suggestion better than the one suggested by abachler?
    Not trying to compare the two solutions; I made a minor change to the source code posted by the OP; and only difference between the two is that of heap and stack.
    Quote Originally Posted by matsp View Post
    And don't say "if the user want's to return the pointer", because that is really a BAD case of not passing it in from the level below.

    --
    Mats
    Nope and I won't say that it's a case of entry not return.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why is it that EVERYONE insists on calling malloc() as soon as pointers are involved. Using malloc for ANYTHING that is smaller than a dozen bytes (at the very least) is a waste of space and time - using it for larger structures (more than, say, 64 bytes) is a waste of time, but reduces use of stack, which may be a good thing at times. Bear in mind that the allocation also takes up EXTRA space beyond the number of bytes you allocated. The smallest I've seen on a 32-bit system is 12 bytes. Most systems, it's 16-32 bytes and can be up to 64 bytes. On top of that, the size is rounded up to some limit, at least 8 bytes in anything reasonably modern. So allocating, like you do, 4 bytes on the stack is likely to take up 40 bytes - and you also need to add the code to deallocate the memory when finished.

    But unless you are writing code for an embedded system with really small stack, using a few hundred bytes of stack is no big deal [unless you are calling the function recursively, or have HUGE number of functions that call each other to very deep nesting levels some other way]. The occasional local variable that takes up 1000-2000 bytes is also no big deal.

    Using malloc to allocate ONE POINTER like this is really not correct at any time - it is ALWAYS badly designed code - the expcption being if ONE POINTER could sometimes be THOUSANDS of pointers.

    Sorry for venting on YOUR post, because you are not the only person suggesting this sort of coding - but really, memory and stack-space really isn't that scarce a resource in modern systems. It may have been different when K&R came up with C some 30+ years ago, and a PDP-11 allowed an application to be 64KB of code and 64KB of data. But modern machines have megabytes if not gigabytes of RAM, and a megabyte or two of stack-space at the least. [Not counting, of course, embedded systems, but the above code indicates windows, and I doubt it is for Win CE]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Well said.
    I have seen one guys program in which he used malloc to creat every variable in his
    program!!

    Perhap it would be better for people to say what they are actually trying to achieve rather
    than posting their mangled code!!
    Last edited by esbo; 01-12-2009 at 11:13 PM.

  12. #12
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by matsp View Post
    Why is it that EVERYONE insists on calling malloc() as soon as pointers are involved.
    This is ingrained into me, thanks to my C book. The book never explains in any detail about what part actual available memory plays in deciding whether to use malloc or not.

    As soon as I think pointers, I automatically think memory needs to be allocated, and that malloc is what's required.

    I would dare say that other C books, as well as C courses would lead the student down this particular path too.

    It's great that you brought this up.

    Maybe this should be a FAQ?

    EDIT: Also, I think most books would try to emphasise "efficient" use of memory. Allocate memory when needed, free it when done. I suppose this is "efficient" use of memory, but not efficient use of resources ie. the processor has to spend time allocating, then freeing.
    Last edited by happyclown; 01-12-2009 at 10:32 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer does not involve malloc. A pointer merely contains a memory address and can point to any valid memory address - whether that address comes from taking the address of a local variable (pass-by-address/pointer, remember!?) or from the heap (malloc).
    And here is a very good rule to remember: if a function wants a pointer, then usually it expects the address of something, not that you stuff in some pointer of equal type containing some bogus value or malloced memory.
    If the function wants to allocate memory, it will do it itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for all the responses.

    Well, I don't want to have to use malloc if it is a waste of time just for one char. However, I guess this would be better.

    Would this be an accepable alternative?
    Code:
    char msg;
    char *error_msg = &msg;
    GetErrorDescription(result, &error_msg);
    Thanks,

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by steve1_rm
    Well, I don't want to have to use malloc if it is a waste of time just for one char. However, I guess this would be better.

    Would this be an accepable alternative?
    You might want to read the documentation on GetErrorDescription(). The second parameter serves as an out parameter in that the "error description" string is likely to be placed there by the function. Considering that it is a pointer to a pointer, it is likely that GetErrorDescription() allocates memory for the "error description" string, and then makes the pointer whose address you pass in point to the first character of that dynamically allocated string. If this is the case, then the expected usage would be along the lines of:
    Code:
    char *error_msg = NULL;
    GetErrorDescription(result, &error_msg);
    if (error_msg)
    {
        printf("Error: %s\n", error_msg);
    }
    free(error_msg);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  3. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 05:14 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM