| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"kazack" |
| Date: |
11 Nov 2003 11:26:40 PM |
| Object: |
Passing Structure to a function |
I am a little confused with code I am looking at. My c++ book does not go
into passing a structure to a function so I pulled out a c book which does.
and I do not understand the prototype verses the actual function call. I
will post the code below of the structure, the prototype and and function
call and if someone can explain this I would be very appreciative:
struct data
{
float amount;
string fname;
string lname;
} rec;
//prototype
void print_rec(struct data x);
//function call
print_rec(rec);
Where I am confused at is with the difference between the prototype and and
the function call. Where does the x come from in the passing parameters??
Thank you,
Shawn Mulligan
.
|
|
| User: "Jon Bell" |
|
| Title: Re: Passing Structure to a function |
12 Nov 2003 09:22:14 AM |
|
|
In article <kwjsb.6709$Bv6.2038122@news1.epix.net>,
kazack <kazack@talon.net> wrote:
struct data
{
float amount;
string fname;
string lname;
} rec;
This does two things:
(1) It defines a data type named 'data', as a struct with the specified
fields.
(2) It declares a variable named 'rec', of type 'data'.
The usual practice in C++ is to separate the two actions:
struct data
{
float amount;
string fname;
string lname;
};
data rec;
This is mainly because data types are usually defined outside of any
function, so they are available in any function in the current scope,
whereas variables are usually declared inside a function, so they are
directly usable only within that function.
To put it another way, types are usually defined globally, but variables
are usually defined locally.
//prototype
void print_rec(struct data x);
The "struct" keyword is completely unnecesary here, in C++. C++ compilers
accept it only for backward compatibility with C.
--
Jon Bell <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.
|
|
|
|
| User: "kazack" |
|
| Title: Re: Passing Structure to a function |
11 Nov 2003 11:56:44 PM |
|
|
Okay here is my code for a simple store first name last name of 2 people
passing it to a function to print both records to the screen. Please let
me know what is wrong with this is anything. The code works with no problem
but is it the proper way of doing it?
Also must a structure be defined outside of main?
The books do not cover that but that is where it is showing all defined
structures at. I did try moving the structure into main with the following
code and got like 6 different compile errors. So I just wanted to know for
sure if this is not the case how would you modify the code to work while
putting a structue inside of main. From everything I was taught so far you
want as little as possible outside of main so that the scope is not global.
#include <string>
#include <iostream>
using namespace std;
void print(struct phonerec);
struct phonerec
{
string fname;
string lname;
}phone[2];
int main()
{
int x;
x = 0;
while(x<=1)
{
cin >> phone[x].fname;
cin >> phone[x].lname;
x++;
}
print(phone[2]);
return 0;
}
void print(struct phonerec)
{
int x = 0;
while (x<=1)
{
cout << phone[x].fname<<" "<<phone[x].lname<<endl;
x++;
}
}
Thank You for your help and understanding once again,
Shawn Mulligan
.
|
|
|
|

|
Related Articles |
|
|