Thread: Struct prototype

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Struct prototype

    Hey guys

    I never use structs really but I was messing around with them and I discovered somthing that my book doesnt teach. The below code will not compile as I havent declared a prototype for the "printNumber" function, but that made me think, what parameter do I place in the prototype to suggest I will be passing a struct to it? I havent covered passing objects to functions yet, so I thought id try with structs before I attempted classes.

    And, is it safer to pass it by reference or pointer reference?

    Code:
    #include <iostream>
    
    struct Number
    {
       int m_Num;
    };
    
    int main()
    {
        Number n;
        
        n.m_Num = 10;
        
        printNumber ( n );
        
        std::cin.get();
        
        return 0;
    }
    
    void printNumber ( Number passStruct )
    {
         std::cout << "Number was: " << passStruct.m_Num;
    }
    Any help appriciated
    Double Helix STL

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I generally just copy the text between the return type and just before the { that defines the body of the function, then add a semicolon. So like this:
    Code:
    void printNumber ( Number passStruct );
    You can omit the parameter passStruct, so this is valid:
    Code:
    void printNumber ( Number );
    But the only side-effect of this that you don't know what the parameter is supposed to stand for, e.g.
    Code:
    void myfunc(int, int)
    says much less than
    Code:
    void myfunc(int id, int age)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks mats. Very helpful.
    Double Helix STL

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, as they say, references are the "C++ way" of passing data. If you can avoid pointers, then it's best to do so.
    And to add to mats, I would use a full prototype (ie, don't leave out argument names) because everyone looking over those prototypes want to see the names and some tools such as intellisense extracts information from the prototypes, so it's always a good thing to leave them in there!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    If you can avoid pointers, then it's best to do so.
    That statement is just asking for an argument.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh? Pointers do have their use, but for passing things over functions, references are preferred before pointers. And I suppose that's all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    references are the "C++ way" of passing data
    That's another statement just asking for an argument.

    The C++ way of passing data is "by value", "by reference", or "by pointer". Which you use is situation-dependent, but saying that one of them is the "C++ way" is just wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM