Thread: Functions and structures in C

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    6

    Functions and structures in C

    Hello everyone. I have been reading "The C Programming Language", and one of the examples seems to not work for me. At least it doesn't work in windows. In linux it works fine, but in windows the following code gives me these errors and warnings:
    Line 11: return type is an incomplete type
    Line 12: storage size of 'temp' isn't known
    Line 15: [Warning]'return' with a value, in function returning void
    Using the Dev C++ compiler latest version.
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        struct point {int x; int y;};
        struct rect {struct point pt1; struct point pt2;};
        
    }
    
    
    struct point makepoint(int x, int y)
    {
           struct point temp;
           temp.x = x;
           temp.y = y;
           return temp;
    }
    Thanks in advance for any help to my problem.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "Does not work" is vague - I'm assuming you mean it won't compile?

    The structure "point" is not defined in the same scope as the function and arguments within that function. Therefore, the function (of return type "struct point") and the variable within the function ("temp") have no definition. If you define the "point" definition as a global variable, it will compile.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    So Matticus why does it work in linux and not windows? The code is also directly from the book and only has problems in windows.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kurse
    So Matticus why does it work in linux and not windows?
    Compiling with gcc on Ubuntu 12.04, with the -Wall -pedantic -std=c99 options:
    Code:
    test.c:12:14: error: return type is an incomplete type
    test.c: In function ‘makepoint’:
    test.c:14:21: error: storage size of ‘temp’ isn’t known
    test.c:17:8: warning: ‘return’ with a value, in function returning void [enabled by default]
    test.c:14:21: warning: unused variable ‘temp’ [-Wunused-variable]
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I have no idea - I'm getting the same errors/warning in Linux.

    [edit] too late!

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Quote Originally Posted by Matticus View Post
    I have no idea - I'm getting the same errors/warning in Linux.

    [edit] too late!
    Strange maybe my compiler is out of date or something.
    I am doing gcc test.c -o test and that works fine.
    I did do what you said and it works now. Strangeness everywhere.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Strange maybe my compiler is out of date or something.
    Dev-c++ is obsolete, and so is the compiler that it comes packaged with.

    Consider either
    Orwell Dev-C++ | Free Development software downloads at SourceForge.net
    Code::Blocks
    smorgasbordet - Pelles C
    The first two are different IDE's, but they both use the GCC compiler underneath.

    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    Copyright (C) 2011 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    From memory, the version of gcc with dev-c++ is something like 2.95, which is now well over a decade old.

    The "-Wall -pedantic" options on gcc are pretty good at keeping you on track.

    Many compilers relax the rules (in different ways). So for example the typedef scope issue in your original post.
    You should regard as "it compiles" as being the first step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Thanks a lot. That makes a lot more sense to me, and now I now of some alternatives which I had been looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help in structures and functions
    By Ebb in forum C Programming
    Replies: 1
    Last Post: 12-07-2011, 07:39 AM
  2. help me with structures using functions
    By magnetpest2k7 in forum C Programming
    Replies: 2
    Last Post: 10-07-2009, 01:32 AM
  3. Functions in Structures
    By pritin in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2007, 01:40 AM
  4. functions and structures
    By subflood in forum C Programming
    Replies: 2
    Last Post: 06-11-2005, 06:15 PM
  5. structures and functions
    By akalvarado in forum C Programming
    Replies: 4
    Last Post: 11-24-2002, 11:09 AM

Tags for this Thread