Thread: Prototypes & Structures

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Prototypes & Structures

    I have three questions here. First, function prototypes such as
    Code:
     void strip_newline(char *str, int size) {}
    appeared out of nowhere, so this is my question about that: What does the * mean, and how does it know that int size is the size of an array?

    Second, in the code used, for that function, the newline and null characters are enclsed in ' ' rather than " ". Why is that?

    Third, structures. They seem to just be more to type. Struct.x = 1 instead of just x = 1. Why not just declare the variables inside main()?

    Thanks!
    CG-20

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. '*' means a pointer. ie, str is a pointer to a character -- that is holds an address of a character. In this case, the start of a character in a string.

    size is how many elements in the array I assume (since you posted no code), by 'the array' I mean how many elements come after the first address pointed to by 'str'.

    2. Single quotes means a single character, double quotes is short-hand for an array of single characters -- nul terminated,
    Code:
    char hello[] = "hello";
    Is the same as
    Code:
    char hello[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    3. They're really blue-prints, it's far more convenient to use a blue-print to build something than start from scratch every time.

    For example, if you wanted 2 x's, y's, z's in main(),
    Code:
    int main(void)
    {
        int x1, x2, y1, y2, z1, z2;
        x1 = 5;
        x2 = 10;
        y1 = 60;
        y2 = 50;
        z1 = 9;
        z2 = 18;
        return 0;
    }
    Hence repeat for n variables.
    It's far easier to write,
    Code:
    struct num_t
    {
        int x, y, z;
    };
    
    int main(void)
    {
        struct num_t one, two;
        one.x = 5;
        one.y = 60;
        one.z = 9;
        two.x = 10;
        two.y = 50;
        two.z = 18;
        return 0;
    }
    See how only 2 variables compared to 6 were declared in main().

    You can then pass the structures to other functions without worrying about what's inside. It turns out to be less typing.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    void strip_newline(char *str, int size) {}
    That's a function.
    Code:
     void strip_newline(char *str, int size);
    That's a prototype.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The key on structures is that they hold a collection of items - such as a rectangle can be defined as x, y, width, height, so you make a struct:
    Code:
    struct rect
    { 
       int x;
       int y;
       int width;
       int length;
    };
    Of course, it's possible to just use 4 variables each time you want to represent a rectangle. But say you wanted to represent 10 rectangles in an array:
    Code:
    int x[10];
    int y[10];
    int width[10];
    int length[10]
    could be used, but isn't it clearer that we have 10 RECTANGLES (rather than some arbitrary set of integer arrays) if we write this:
    Code:
    struct rect rectangles[10];
    Also, when passing arguments to functions, structures help, because there is only one parameter, instead of several:
    Code:
    void drawRectangle(struct rect r, int colour)
    {
        MoveTo(r.x, r.y);
        LineTo(r.x+r.width, r.y);
        LineTo(r.x+r.width, r.y + r.height);
        LineTo(r.x, r.y + r.height);
        LineTo(r.x, r.y);
    }
    --
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CodeGeek20 View Post
    I have three questions here. First, function prototypes such as
    Code:
     void strip_newline(char *str, int size) {}
    appeared out of nowhere, so this is my question about that: What does the * mean, and how does it know that int size is the size of an array?

    Second, in the code used, for that function, the newline and null characters are enclsed in ' ' rather than " ". Why is that?

    Third, structures. They seem to just be more to type. Struct.x = 1 instead of just x = 1. Why not just declare the variables inside main()?

    Thanks!
    CG-20
    I'm going to answer in metahpors today:
    1. If I give you a lemon and a lemon squeezer, how do I know you're going to squeeze me some lemon juice? The answer is of course that I don't know that, but it would be the most sensible thing for a person to do with those items. You could of course do whatever you wanted with them though.

    2. This has been answered.

    3. Which would you rather have: Ten boxes of 500-piece jigsaw puzzles, or 5000 assorted jigsaw puzzle pieces all of which belong to one of ten puzzles?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    haha i like those answers iMalc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Data Structures Debugging
    By 0rion in forum C Programming
    Replies: 15
    Last Post: 08-28-2004, 10:36 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM