Assign char*[] to void**

So I'm using an old API and it has a problem I don't know how to solve.
It's something like this.

1
2
3
4
5
6
7
8
9
10
int function(int selector, void **value) {
    
    char *names[32];
    long length = getNames(names); // Returns number of elements, length is used for something else
    
    // HERE IS THE PROBLEM, value does not get any of the names
    value = (void**)names;

    return 0; // Returns error code
}
try casting a type to value?
What do you mean?
You cannot change the value of a passed parameter (it is a local variable). You can only change the content (if possible of that parameter).

In your case you may pass a reference:
1
2
3
4
5
6
7
8
9
10
int function(int selector, void **&value) { // Note & in order to change value
    
    char *names[32];
    long length = getNames(names); // Returns number of elements, length is used for something else
    
    // HERE IS THE PROBLEM, value does not get any of the names
    value = (void**)names;

    return 0; // Returns error code
}


Oh pass by reference, I see. The casting word threw me off.
One more thing i forgot to mention: You cannot assign the local variable names to the a non local variable (such as value). As soon as function(...) ends the life time of names also ends. The content of value when returning is undefined.

Generally: Do not return pointers to local variables.

So if getNames(...) returns pointers to local variable as well it would be undefined as well.
This actually works. Everything is returned correctly. And that void** has the value of everything.

1
2
3
4
5
6
7
8
9
10
11
12
13
		if (!asioDrivers)
			asioDrivers = new AsioDrivers;

		char *driverNames[MAX_NUMBER_OF_DRIVERS];
		long length = asioDrivers->getDriverNames(driverNames, MAX_NUMBER_OF_DRIVERS);

		for (size_t index = 0; index < MAX_NUMBER_OF_DRIVERS; index++) {
			if (index < length)
				value[index] = driverNames[index];
			else
				value[index] = NULL;
		}
		return true;
Last edited on
Topic archived. No new replies allowed.