Cyberflow Cyberflow - 1 year ago 70
C Question

C - Working with pointer to pointer has arguments in fuction

I'm learning and trying to understand properly this part of working with C.

I am doing an exercise and have to write a function that returns an image but has a pointer to pointer that has to store a value in case there's a error.

FYI: The function is stored in a module that is included by the main file.

Here's a signature declaration of my code:

Img imageFromFile(const char * filename, char ** messageErreur) {

if (error)
messageErreur = 1; //Trying to pass 1 has error code out of the function


And i keep on getting this message when I compile :

img.c:148:23: attention : assignment makes pointer from integer without a cast
messageErreur = 1; //Trying to pass 1 has error code out of the function

I don't know what i'm note to sure what i need to declare in the main file to pass along this function.

It's a pointer to a pointer so i guess I'll need to create a pointer of type char[]?

The pass the address "&pointer" when calling the function?

Answer Source

The caller is providing a pointer to a char pointer, in other words, you have been granted the ability to return a char* back to the caller. To do this, first dereference the char** and then assign the char* you want to return:

*messageErreur = "some string";
*messageErreur = someRuntimeAllocatedStringBuffer;

Always be careful that if the caller was given the pointer to "some error string" they must not modify it (and typically would not for a returned message).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download