-
February 21st, 2010, 12:59 PM
#1
How do you pass a struct with an array member?
How do you pass a struct with an array member to another function? This is what i have so far.
People are telling me to use an array container ([] or *) but I have no idea how to implement this in my situation.
Code:
#include <iostream>
using namespace std;
int edit(struct Student data);
struct Student{
int number;
char name[10];
double money;
};
int main(){
struct Student info[2];
int i;
for(i=0;i<2;i++){
cin >> info[i].number >> info[i].name >> info[i].money;
}
i=0;
while(i < 2){
edit(info);
}
cout << info[1].number << info[1].name << info[1].money << endl;
return 0;
}
int edit(struct Student data){
data[0].number = 666;
data[0].name = "TEST";
data[0].money = 99.99;
cout << data[0].number << data[0].name << data[0].money << endl;
return 0;
}
-
February 21st, 2010, 01:12 PM
#2
Re: How do you pass a struct with an array member?
int edit(struct Student& data)
{
}
If you are working with C:
int edit(struct Student* data)
{
}
-
February 21st, 2010, 01:18 PM
#3
Re: How do you pass a struct with an array member?
Originally Posted by djgirotto
How do you pass a struct with an array member to another function? This is what i have so far.
Well, what you have is fine in principle, though it may not do what you want. You are passing a Student object to the function edit by value. That means that a copy of the object is made and the function works on the copy. If you want the function to work on the original object, you'll have to pass it by reference (or const reference if you don't want to alter the object). E.g.
Code:
void Init(Student& data)
{
data.number = 0;
data.name[0] = '\0';
data.money = 0.;
}
void Display(const Student& data)
{
std::cout << data.number << ' ' << data.name << ' ' << data.money << '\n';
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 21st, 2010, 03:19 PM
#4
Re: How do you pass a struct with an array member?
Does this make sense?
Code:
#include <iostream>
using namespace std;
void edit(Student *data);
struct Student{
int no;
char name[10];
double money;
}
int main(){
struct Student data[2];
data[0].no = 5;
data[0].name = "Mike";
data[0].money = 10.50;
edit(Student &data);
cout << data[0].no << data[0].name << data[0].money << endl;
cout << data[1].no << data[1].name << data[1].money << endl;
return 0;
}
void edit(Student *data){
*data[1].no = 10;
*data[1].name = "Test";
*data[1].money = 99.99;
}
Is this possible?
Expecting it to print out...
>5Mike10.50
>10Test99.99
>
Last edited by djgirotto; February 21st, 2010 at 03:23 PM.
-
February 21st, 2010, 04:44 PM
#5
Re: How do you pass a struct with an array member?
I would think that this:
Code:
data[0].name = "Mike";
Wouldn't even compile. There is no = operator for a char array. Try strcpy or use a std::string.
-
February 22nd, 2010, 12:20 AM
#6
Re: How do you pass a struct with an array member?
I am still unclear as to how I would pass an array of structs to functions. Could anyone provide me with some assistance?
Also.. if I want to compare user's integer input, if they input a value and if I want to test if it's 0 or not how would I do that?
Last edited by djgirotto; February 22nd, 2010 at 12:34 AM.
-
February 22nd, 2010, 01:20 AM
#7
Re: How do you pass a struct with an array member?
Code:
void edit(Student *data){
(*data)[1].no = 10;
// ...
}
-
February 22nd, 2010, 04:03 AM
#8
Re: How do you pass a struct with an array member?
Originally Posted by djgirotto
Does this make sense?
Is this possible?
Expecting it to print out...
>5Mike10.50
>10Test99.99
>
Most people used to coding in C++ would not use arrays anyway, but an STL containers instead.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student{
int no;
string name;
double money;
};
void edit(vector<Student> &data);
int main(){
vector<Student> data(2);
data[0].no = 5;
data[0].name = "Mike";
data[0].money = 10.50;
edit(data);
cout << data[0].no << data[0].name << data[0].money << endl;
cout << data[1].no << data[1].name << data[1].money << endl;
return 0;
}
void edit(vector<Student> &data){
data[1].no = 10;
data[1].name = "Test";
data[1].money = 99.99;
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 22nd, 2010, 04:44 PM
#9
Re: How do you pass a struct with an array member?
Unfortunately the project I'm working on has a set requirement which states we must use arrays, don't know anything about vectors at the moment.
-
February 22nd, 2010, 04:52 PM
#10
Re: How do you pass a struct with an array member?
When passing any sort of array to a function, you should always pass the length of the array as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|