• 0

I've got a question for a C++ programmer!


Question

I'm stuck on a homework assignment i have. (i know i know, do your own work and all that) but its just one part i'm having trouble with. Hopefully, someone could just throw out a few ideas for me.

I have a an array of structs with some records in it, and i need to pass one record at a time (not my whole array of structs) into another function to do some computations. I've been struggling with this all day.

So: how can I pass one item at a time (with a loop) into a function?

-thanks!

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

for(i=0;i<4;i++)

{

compute(employee)

}

//thats what my loop looks like, but it gives me an error saying cannot convert parameter 1, no user defined conversion available.

thanks for the quick reply though!

Link to comment
Share on other sites

  • 0
That is good but make sure to pass all the info as pointers to the array. Not the actual array itself.

It's not passing the array. It's passing a single struct from the array. Granted, it's passing by value, but structs should be lightweight anyhow. I wouldn't be too concerned with performance for a school assignment.

Link to comment
Share on other sites

  • 0
It's not passing the array. It's passing a single struct from the array. Granted, it's passing by value, but structs should be lightweight anyhow. I wouldn't be too concerned with performance for a school assignment.

He could pass a pointer easily:

function(array+i);

Pointer arithmetic is a good thing :)

Link to comment
Share on other sites

  • 0
He could pass a pointer easily:

function(array+i);

Pointer arithmetic is a good thing :)

I beg to differ. :no:

Then again I'm a java based programmer and dont use pointers, but rather references. I find pointer harder to read

function(array+i) is harder to read than funtion(array)

you also cant tell array is an "array" without the [] as easily

Link to comment
Share on other sites

  • 0
I beg to differ. :no:

Then again I'm a java based programmer and dont use pointers, but rather references. I find pointer harder to read

function(array+i) is harder to read than funtion(array)

you also cant tell array is an "array" without the [] as easily

array+i and array aren't the same thing. *(array+i) and array are. When in C/C++ I actually try to derefernce myself so pointers make more sense to me.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.