How do I properly turn a const char* returned from a function into a const char** in C?

From the added information, it seems what you are trying to do is call a function which wants to return a string through one of the function arguments. The best way to do this in my opinion would be something like: const char* fileName; config_setting_lookup_string(..., &fileName); (...) return fileName This will allocate room for a const char* on the stack. The function call will fill the pointer with the address of the string it wants to return.

This pointer value can then be passed out of the function if needed (unlike the pointer to the pointer, which would point to the stack, and be invalid when the function returns). Note that initializing fileName with "getString()" would presumably leak memory, since the pointer to the returned string would be overwritten, and the string never deallocated.

From the added information, it seems what you are trying to do is call a function which wants to return a string through one of the function arguments. The best way to do this in my opinion would be something like: const char* fileName; config_setting_lookup_string(..., &fileName); (...) return fileName; This will allocate room for a const char* on the stack. The function call will fill the pointer with the address of the string it wants to return.

This pointer value can then be passed out of the function if needed (unlike the pointer to the pointer, which would point to the stack, and be invalid when the function returns). Note that initializing fileName with "getString()" would presumably leak memory, since the pointer to the returned string would be overwritten, and the string never deallocated.

If you're calling a function that needs a const char**, you could do it like this: const char *s = getString(); myFunction(&s); Since s is allocated on the stack in the above example, if you want to return a const char** from your function, you will need to put it on the heap instead: const char **sp = malloc(sizeof(const char *)); *sp = getString(); return sp; HTH.

String in your case is a local, so taking the address of it is a bad idea since the memory for the local can (and likely will be) re-used for other purposes when you leave the method. In general, it is not a good idea to use the address of a local variable outside of its scope. What are you trying to achieve?

Should have added that before. Thanks for your noob patience. Edited now.

According to the documentation, "Storage for the string returned by config_lookup_string() is managed by the library and released automatically when the setting is destroyed or when the setting's value is changed; the string must not be freed by the caller. " hyperrealm. Com/libconfig/libconfig_manual.

Html#The-C-API – spirulence May 9 '10 at 6:07.

No, you can't change config_setting_lookup_string() in the way you've described. You're returning a pointer to the string variable, but as soon as that function ends that variable goes out of scope and is destroyed. You can, however, fix your initial problem quite easily.

Leave the definition of config_setting_lookup_string() as it is, and call it like so: const char *fileName = NULL; config_setting_lookup_string(foo, "bar", &fileName).

You need the two lines. However, string is a local variable on the stack, once it goes out of scope, you may not have a pointer to the data returned by getString().

I like nornagon and caf's solution, const char *fileName; config_setting_lookup_string(foo, "bar", &fileName); but if you can change config_setting_lookup_string you could also do it this way: int config_setting_lookup_string(..., const char *&value) { ... const char *string = config_setting_get_string(member); value = string; ... } const char *fileName; config_setting_lookup_string(foo, "bar", fileName).

If you return stringPtr, you will be returning a pointer to a local variable (string). So no, you can't do that. Why are you trying to do this?

That might allow us to make better suggestions. Update: Okay, now I see what you're trying to do. You're doing it wrong: value = &string; If value is meant as an output parameter, the above line cannot work because you're assigning to a local variable.

Don't let the extra level of indirection confuse you. If you were writing a function that had an output parameter of type T, you'd write it as: void foo(T* value) { *value = GetT(); } Now replace T with const char*: ... *value = string; ... And now you're not involving any temporary, local variables. Of course, that's how the code was originally written (and that part of it was correct), so that doesn't really help you.To address your intent, you should: Make config_setting_lookup_string do assert(value!

= NULL). Audit the callers of the function and fix them to stop passing garbage. They should be doing: const char* foo; config_setting_lookup_string(..., &foo); and NOT: const char** foo; config_setting_lookup_string(..., foo).

Edited and added the full context of what I'm trying to do. Basically, I'm trying to rewrite a library routine that returns a string through a const char ** to include some boilerplate initialization code which suits me better. – spirulence May 9 '10 at 6:44.

To make things short, this function will take name, check some conditions and append to it a postfix, making it a proper filename. Name = "someFile", return = "someFile-hd. The problem I have, is that when I create a const char* in my function it will have a scope limited to the function it was created in.

On the other hand, I can't modify the code of loadFile function, since It's a 3rd party library I am using. I can write a wrapper around loadFile to delete the const char* I've created, but I would not like to do that, since this is a porting exercise and this would break some templates I've created to make it easier. Is there a way to write this function like I've described?

GetFileName is a function I am trying to write, I can change the return type, but it has to fit as a parameter to loadFile function. My main problem is that const char* returned by getFileName is either lost because it's out of scope or I have to delete it explicitly, which breaks my porting template. I would like to generate the filename in one function call and do nothing else about it in other words, in original code I have loadFile("something.

Png") and I want to change it to loadFile(getFileName("something")) without adding any new lines after that.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions