tags:

views:

112

answers:

3

EDIT: An initial "float** floats;" was a typo, corrected. Also, "*floats[0] = 1.0;" does work, but "*floats[1] = 1.0;" segfauls at that point. The array is malloced. Compilation has no warnings on pedantic c99.

This worked:

(pseudo:)

void func(int*);
int i;
func(&i);
void func(int* i) {
 func2(i);
}
void func2(int* i) {
 *i = 2;
}

initial value changed to 2.

but this didn't work:

void func(float**);
float* floats;
func(&floats);
void func(float** floats) {
 func2(floats);
}
void func2(float** floats) {
 *floats[1] = 1.0;
}

segfault on assignment;

+2  A: 

Why are you passing a pointer to an array? Have you tried

func(float *floats)
{
  func2(floats);
}

func2(float *floats)
{
  floats[0] = 2;
}
Brandon Horsley
Not the issue; the point is to manipulate external to the functions variables.
Lela Dax
I understand, but arrays are passed as pointers, you don't pass a pointer to a pointer for an array unless you intend to change the address of the allocated memory. Try my solution and let me know if it works for you.
Brandon Horsley
+3  A: 

because 'floats' is a bad pointer. Sure, you declared the pointer, but you haven't allocated any memory, so wherever it happens to point will be invalid.

Also, the address of a float** is a float***, which is not what your function calls for.

Ed Swangren
Dude, post your real code. Don't make people waste time debugging transcription errors and omissions.
Tyler McHenry
What are you talking about? If you can't understand something as simple as what I said above then maybe you should start with a beginner's tutorial.
Ed Swangren
A: 

Say you have these 2 funcs, like you posted:

void func2(float** floats) {
 *floats[1] = 1.0;
}
void func(float** floats) {
 func2(floats);
}

Then you have the following code:

float* floats;
func(&floats);

What happens? First, floats is a float *, and since it's not assigned, it points to a random memory location, say 0xBADD. If you write to here, you're liable to screw something up.

The address of floats, &floats is another memory location pointing to where it is on the stack, say 0x1000. This is passed to func, which is then verbatim passed to func2.

What does func2 do? It tries to write something in the location *floats[1]. I'm not sure what happens first, the bracket or the star, so let's take both cases:

*(floats[1]): First you find what floats[1] is. Since floats is the memory location of the initial floats variable, this is 0x1000. floats[1] would be 0x1004, since pointers are 4 bytes wide assuming a 32-bit system. Then you try to dereference this pointer, which is the value of whatever is in memory location 0x1004. This could be anything, like 0xBAD2, so trying to write a float value there most likely causes a segfault.

(*floats)[1]. Here you first dereference floats, which gets you 0xBADD, of type float *. You then try to write a value into 0xBADD + 4, or 0xBAE1. This is memory that hasn't been allocated for you, so writing there will likely cause a segfault.

Either way, you're messing with memory that isn't yours, thus leading to a segfault.

Claudiu
The array is malloced; it's pseudo code.
Lela Dax
oh.. well say `float *floats = ...;` or something! in that case you would want `(*floats)[1]`, or just pass the array directly like in the accepted answer.
Claudiu