Re: function prototype - c++

This is a discussion on Re: function prototype - c++ ; Art Cummings wrote: > 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 ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Re: function prototype

  1. Default Re: function prototype

    Art Cummings wrote:

    > 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

  2. Default Re: function prototype

    Default User wrote:

    > Art Cummings wrote:
    >
    > > 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);


    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


  3. Default Re: function prototype

    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 B. Kopec (tkopec@NOSPAMPLEASElife.pl)
    If I told you you had a beautiful body, would you hold it against me?

  4. Default Re: function prototype

    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" <tkopec@NOSPAMPLEASElife.pl> wrote in message
    > news:47546ff8$1@news.home.net.pl...
    > > 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


  5. Default Re: function prototype

    Art Cummings wrote:

    > 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

  6. Default Re: function prototype

    Art Cummings wrote:

    > 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.


    > > 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.




    Brian

  7. Default Re: function prototype

    Art Cummings wrote:
    > 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[i];
    }
    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[i];
    }
    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

    Else thread Art Cummings wrote:
    > 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

  8. Default Re: function prototype

    Art Cummings wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask



  9. Default Re: function prototype

    Art Cummings wrote:

    > 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

  10. Default Re: function prototype

    Art Cummings wrote:

    > 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

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Re: function prototype
    By Application Development in forum c++
    Replies: 0
    Last Post: 12-04-2007, 10:54 AM
  2. What should be the prototype of this function ? (noobie)
    By Application Development in forum C
    Replies: 6
    Last Post: 10-19-2007, 12:36 PM
  3. What should be the prototype of this function ? (noonie)
    By Application Development in forum C
    Replies: 4
    Last Post: 10-19-2007, 11:58 AM
  4. Self referencing function prototype typedef
    By Application Development in forum C
    Replies: 2
    Last Post: 07-19-2007, 08:19 AM
  5. Calling clarion DLL function from C++ prototype
    By Application Development in forum Clarion
    Replies: 3
    Last Post: 05-12-2006, 10:27 PM