Functions and structures in C

This is a discussion on Functions and structures in C within the C Programming forums, part of the General Programming Boards category; Hello everyone. I have been reading "The C Programming Language", and one of the examples seems to not work for ...

  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
    2,571
    "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
    21,935
    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]
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,571
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 06: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, 10:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21