×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

*var

*var

(OP)
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

RE: *var

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.

RE: *var

(OP)
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

RE: *var

Probably a typo in the book - report it to the author: you may get something for your efforts.

RE: *var

What if var is an array of floats?

RE: *var

Quote (melone):

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.

RE: *var

(OP)
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

RE: *var

(OP)
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

RE: *var

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) { *var += 1; }

RE: *var

CODE

example(float *var){var += 1;}
Is this the example as written, or are there more statements after the increment?

After the increment, var 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.

RE: *var

(OP)
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

RE: *var

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close