Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*var

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
I'm working in another language and drawing from textbook examples in C.

This isn't a problem, but I do have one confusing obstacle. What is a *var?

example(float *var){var += 1;}

Many thanks for your replies!

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
var is a float* (i.e. a pointer to a float).
That example function is completely useless though, since it increments the local pointer in the function, but that has no affect on the float that's passed into the function.
 
That's what I thought, yet that is what the book is doing (as I read it..)

Thanks though! :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Probably a typo in the book - report it to the author: you may get something for your efforts.
 
melone said:
What if var is an array of floats?
What if it's an array of ints casted to float*?
It doesn't make a difference what it is outside the function, since the function isn't touching the data that's being passed in.
 
Maybe the example is incomplete (maybe it assumes something outside). It's on the topic of image manipulation and it probably is an array of floats.

Assuming the example is incomplete, what would var+=1 do where var is a pointer to an array of floats?

Thanks very much! :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
var; // array of floats
var[0]; // first member

var += 1; // shifts array ????
var[0]; // 2nd member ????

Yeah, I did look at a tutorial and I cannot test the examples so I'm guessing. I'm sure things would make sense to me if you shared with me your wisdom ;)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
var += 1; would increment the pointer to the next float in the array; but since var is passed as a float*, it makes a copy of the pointer that is passed to the function and increments that local copy of the pointer. Neither the data values or the pointer value outside the function is changed.
My guess is that they wanted to add 1 to the value, not the pointer, so it should have been like this:
Code:
void example(float *var) { [COLOR=red]*[/color]var += 1; }
 
Code:
example(float *var){var += 1;}
Is this the example as written, or are there more statements after the increment?

After the increment, [tt]var[/tt] will point to the second value of the array that was passed in. If there are statments after the increment, they will take this into account.

If there's nothing else after that statement, then cpjust has said it all; the function does nothing worthwhile.
 
Indeed, there are more statements following :$

I was naive and did not realise the implications. I don't copy anything because then I don't remember why, but the chronology is as..

float sub(int x, float *var){
var += x;
return var[0] + var[1] + var[2] + var[3];
}

Just to be clear I got it, would the equivalent in Java would be...

float sub(int x, float[] var){
return var[x] + var[++x] + var[++x] + var[++x];
}

Thanks!

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Code:
return var[x] + var[++x] + var[++x] + var[++x];
Using multiple pre or post-increments in one statement would be undefined behavior in C/C++ and I would imagine in Java as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top