function prototype

A

Art Cummings

Hello all,

I'm trying to write a function prototype that includes an array of
structures. I'm not sure how to write it. I'm also not sure what the call
to a function that uses this prototype would look like. From what i've read
on the web, it needs to be a pointer to an array. I'm not sure even where
to begin. I've written function protoypes for arrays and 2d arrays but when
I write it for the structure, i get compiler errors.

any help...

Thanks
Art
 
D

Default User

Art said:
Hello all,

I'm trying to write a function prototype that includes an array of
structures. I'm not sure how to write it.

struct st
{
int member;
}

void f(st *data);
I'm also not sure what
the call to a function that uses this prototype would look like.

st array[20];

f(array);

From what i've read on the web, it needs to be a pointer to an array.

No.
I'm not sure even where to begin. I've written function protoypes
for arrays and 2d arrays but when I write it for the structure, i get
compiler errors.

Show us the code.




Brian
 
D

Default User

Default said:
struct st
{
int member;
}

void f(st *data);

Oh, I forgot to mention, you'd be far better off with vectors:

#include <vector>

struct st
{
int member;
}; // forgot the ; above, sorry

void f(std::vector<st> &data);




Brian
 
A

Art Cummings

Here's the code I have.

I get an undefined reference to getInfo, error.

Thanks
Art

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h> //necessary for the _getch function

using namespace std;



const int SIZE = 15; //size for all arrays in student structure

struct Student
{
char fName[SIZE]; //array for first name
char lName[SIZE]; //array for last name
char attendance[SIZE]; //array for attendance
int days[SIZE]; //array for days
};

void getInfo(Student[]);


int main()
{
Student studentInfo[30]; //define an array of 30 students
getInfo(studentInfo);
system("PAUSE");
return 0;
}
void getInfo(Student &s)
{
}
 
A

Art Cummings

Tadeusz, that's exactly the problem, i'm not sure how to define the header
or the call or the function that receives it. I'm not sure when dealing
with an array of structures exactly what this looks like for the prototype,
call and function header. I've not got any examples in my text and that is
exactly what i'm trying to find out. What i'm saying is I don't know how to
define it.

Thanks

Art

Tadeusz B. Kopec said:
Here's the code I have.

I get an undefined reference to getInfo, error.

Thanks
Art

[snip]
Your declaration is:
void getInfo(Student[]);
[snip]

Your definition is:
void getInfo(Student &s)
{
}

Can you see the difference in argument type?
 
D

Default User

Art Cummings wrote:
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the FAQ.

Rearranged.
Tadeusz B. Kopec said:
Here's the code I have.

I get an undefined reference to getInfo, error.

Thanks
Art

[snip]
Your declaration is:
void getInfo(Student[]);
[snip]

Your definition is:
void getInfo(Student &s)
{
}

Can you see the difference in argument type?
Tadeusz, that's exactly the problem, i'm not sure how to define the
header or the call or the function that receives it. I'm not sure
when dealing with an array of structures exactly what this looks like
for the prototype, call and function header. I've not got any
examples in my text and that is exactly what i'm trying to find out.
What i'm saying is I don't know how to define it.

Your function definition should the same parameter types as the
relevant declaration (if you have one). Your declaration specifices a
function getInfo() taking a pointer to Student, which you then call
correctly, but then you DEFINE getInfo() as taking a reference to
Student.

Because C++ allows function overloading, it's not an error to have them
disagree, it just means that you are referring to two different
versions of getInfo(), one which you declared and used, another that
you defined.

At link time, the implementation couldn't find a definition for the
version of getInfo() that you called.

Make the two signatures agree. Then look into vectors, so you need
magic numbers for the size of things.



Brian
 
D

Default User

Art said:
Thanks Brian, we haven't dealt with vectors yet.

You really ought to start with those, and use raw arrays when you are
more experienced.




Brian
 
A

Art Cummings

Thans Tadeusz, how do I define my function header as a pointer to an array
of Students? That's the gist of my confusion. I don't know how to define a
function prototype that is an array of structures to a pointer.

Art
Default User said:
Art Cummings wrote:
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the FAQ.

Rearranged.
Tadeusz B. Kopec said:
On Mon, 03 Dec 2007 15:58:03 -0500, Art Cummings wrote:

Here's the code I have.

I get an undefined reference to getInfo, error.

Thanks
Art

[snip]
Your declaration is:
void getInfo(Student[]);
[snip]

Your definition is:
void getInfo(Student &s)
{
}

Can you see the difference in argument type?
Tadeusz, that's exactly the problem, i'm not sure how to define the
header or the call or the function that receives it. I'm not sure
when dealing with an array of structures exactly what this looks like
for the prototype, call and function header. I've not got any
examples in my text and that is exactly what i'm trying to find out.
What i'm saying is I don't know how to define it.

Your function definition should the same parameter types as the
relevant declaration (if you have one). Your declaration specifices a
function getInfo() taking a pointer to Student, which you then call
correctly, but then you DEFINE getInfo() as taking a reference to
Student.

Because C++ allows function overloading, it's not an error to have them
disagree, it just means that you are referring to two different
versions of getInfo(), one which you declared and used, another that
you defined.

At link time, the implementation couldn't find a definition for the
version of getInfo() that you called.

Make the two signatures agree. Then look into vectors, so you need
magic numbers for the size of things.



Brian
 
D

Default User

Art said:
Thans Tadeusz, how do I define my function header as a pointer to an
array of Students? That's the gist of my confusion. I don't know
how to define a function prototype that is an array of structures to
a pointer.

I wrote the post you replied to, AND you ignored what I wrote about
top-posting. See below.




Brian
 
L

LR

Art said:
Here's the code I have.

I get an undefined reference to getInfo, error.

Thanks
Art

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h> //necessary for the _getch function


using namespace std;

I know that I'll be disagreed with, but I dislike using namespace std;

const int SIZE = 15; //size for all arrays in student structure

struct Student
{
char fName[SIZE]; //array for first name
char lName[SIZE]; //array for last name
char attendance[SIZE]; //array for attendance
int days[SIZE]; //array for days
};


I am somewhat confused by what Student is, or more to the point, why
SIZE is the same for fName, lName, attendance and days.


Are you trying to make a struct that has information for SIZE students?
Or is this information for just one student?

If its for one student think about:
const int NameSize = 15;
const int NumberOfDays = 15;
or something. Because I think it's a little confusing for the size of
the first name and last name to be the same symbolically as the size of
attendance and days.


If its for more than one student, then I think you're going to have
trouble storing the names.

Also, your comments are less than helpful. Sorry. Don't mean to be
brusque, but I know those things are arrays. In the case of attendance
and days, I'm not really certain what they're for. And lName and fName
might be clearer as lastName and firstName or something.


void getInfo(Student[]);

getInfo is a function taking an array of Student as an argument and
returning void.
int main()
{
Student studentInfo[30]; //define an array of 30 students

Magic numbers are generally frowned upon.
getInfo(studentInfo);
system("PAUSE");
return 0;
}
void getInfo(Student &s)
{
}

But here, getInfo is a function taking a reference to a Student as an
argument and returning void.


Forget about structs for a while. Too complicated. Just do an array.

Personally, I think it saves time in small test programs to just put the
functions I'm calling before main. But since you specifically want to
think about prototypes...

So here's a simpler example.

#include <iostream>

int sum(const int [], const int);

int main() {
static const int x[] = { 5,7,9,};
const int s = sum(x, sizeof(x)/sizeof(x[0]));
std::cout << s << std::endl;
}

int sum(const int a[], const int n) {
int result = 0;
for(int i=0; i<n; i++) {
result += a;
}
return result;
}

Now, a struct is easy.

#include <iostream>

struct T {
int t_;

T &operator+=(const T &t) {
t_ += t.t_;
return *this;
}
};

T sum(const T [], const int);

int main() {
static const T x[] = { {5},{7},{9},};
const T s = sum(x, sizeof(x)/sizeof(x[0]));
std::cout << s.t_ << std::endl;
}

T sum(const T a[], const int n) {
T result = {0};
for(int i=0; i<n; i++) {
result += a;
}
return result;
}


Now, I don't want to say that the above code is any good. I don't think
it is. It's just for illustration. Just something to get you started.

HTH
> I've not got any examples in my text and that is
> exactly what i'm trying to find out.

What text are you using?

Did your text cover std::vector<> yet?



LR
 
A

Art Cummings

Thanks Lr, thanks everyone. I'm still not getting how to do this. It seems
that everyone who answers is fairly advanced in understanding c++. I'm a
beginning c++ student. You guys might find it hard to believe, but i'm
doing the best in my class, i've actually got a high 90, so it's not for
lack of trying or effort, it's just that there are alot of new concepts and
just trying to keep those straight is a huge effort at this time. So when I
ask the question, i'm totally thrown for a loop (no pun intended) when
anyone suggest new concepts that we haven't learned. I'm just trying at
this point to get a foundation. So i'll ask the question again, thanks for
everyone who answered.

The structure is Student and I want to define an array of these structures.
The structure contains 4 arrays, one for first and last name, one for
attendance and one for day. What i'd like to do is pass the array of
structures to a function. In order to do this, I need to write a function
prototype, a call statement and a function header, i'm not sure how to do it
however. I've gotten feedback and tried to adjust my code. Note the
adjustments in this recent posting. I'm still not getting it to compile. I
do think that once over this hurdle, i.e. passing the array of structures,
that i'll be on my way, I just don't seem to be able to get that first step.

Thanks
Art



#include <iostream>
#include <fstream>
#include <string>
#include <conio.h> //necessary for the _getch function

using namespace std;



const int SIZE = 15; //size for all arrays in student structure
const int ARRAYSIZE = 30; //size of array

struct Student
{
char fName[SIZE]; //array for first name
char lName[SIZE]; //array for last name
char attendance[SIZE]; //array for attendance
int days[SIZE]; //array for days
};

void getInfo(Student[],int);


int main()
{
Student studentInfo[ARRAYSIZE]; //define an array of 30 students
getInfo(studentInfo,ARRAYSIZE);
system("PAUSE");
return 0;
}
void getInfo(Student &s,int x)
{
}
 
V

Victor Bazarov

Art said:
Thanks Lr, thanks everyone. I'm still not getting how to do this. [..]
void getInfo(Student[],int);

void getInfo(Student &s,int x)
{
}

The two declarations have to MATCH. The former declares that 'getInfo'
expects a pointer of 'Student' type, and an int. The latter declares
that 'getInfo' expects a REFERENCE to 'Student', and an int. In order
to make them match, you need the second one say

void getInfo(Student s[], int x)

You may need to pay a bit more attention to declarations (and what they
mean), otherwise C++ is going to be very difficult for you.

V
 
A

Art Cummings

Thanks V.

You may need to pay a bit more attention to declarations (and what they
mean), otherwise C++ is going to be very difficult for you.

I'm re-considering that dish washing job.

Art
 
D

Default User

Art said:
Thanks Lr, thanks everyone.

I asked you twice not to top-post, yet you continue to do so. Why
should anyone help you when you have demonstrated extreme rudeness to
the group?



Brian
 
A

Art Cummings

Brian, I thought top posting was where you include the previous message in
your reply.


Art
 
A

Art Cummings

Default User said:
I asked you twice not to top-post, yet you continue to do so. Why
should anyone help you when you have demonstrated extreme rudeness to
the group?



Brian

Brian I just got this off of wikipedia. Is what you mean by top posting,
not including the original message and just putting your own message in? So
clarity would help. Honesly if I know the protocol, i'll endeavor to follow
it.

Thanks
Art

The main options are top-posting - replying above the original message;
bottom-posting - replying below; or interleaved posting. While each online
community differs on which styles are appropriate or acceptable, within any
community the use of the "wrong" method risks being seen as a major breach
of netiquette, and can provoke vehement response from community regulars.
 
D

Default User

Art said:
Brian, I thought top posting was where you include the previous
message in your reply.

Here's the information I posted twice already.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the newsgroup FAQ.




Also, see this:

<http://www.caliburn.nl/topposting.html>




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top