Why isn't this an error?

This is a discussion on Why isn't this an error? within the C Programming forums, part of the General Programming Boards category; Code: #define MEMBER_SIZE 500 struct table{ double num; struct table *next_table; } struct table member_tables[MEMBER_SIZE]; Wouldn't the first struct give ...

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    Why isn't this an error?

    Code:
    #define MEMBER_SIZE 500
    
    struct table{
       double num;
       struct table *next_table;
    }
    
    struct table member_tables[MEMBER_SIZE];
    Wouldn't the first struct give an error since it's declaring itself in the middle of it's own declaration?

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Cell View Post
    Code:
    #define MEMBER_SIZE 500
    
    struct table{
       double num;
       struct table *next_table;
    }
    
    struct table member_tables[MEMBER_SIZE];
    Wouldn't the first struct give an error since it's declaring itself in the middle of it's own declaration?
    this is the typical syntax to write a linked list in C. As for whether or not the compiler should complain, all you're really telling the compiler is that you are declaring another variable, in this case a pointer thats gonna hold the addy of a struct table type. so the short answer is no. I'm sure someone more knowledgeble on here could give you the loooonnnnnger answer.
    Last edited by caroundw5h; 03-31-2009 at 11:28 PM. Reason: too tired to think ;(
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    next_table is a pointer to a table, not a table
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    I see what you guys are saying. Because *next_table is creating a pointer of type table, not a new table, it is valid.

    caroundw5h, the link you included seems to just be an overview of stdio.h. Do you have a good source regarding linked lists in this manner?

    Thanks!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, the posted code is missing a semicolon.

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

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Cell View Post
    I see what you guys are saying. Because *next_table is creating a pointer of type table, not a new table, it is valid.

    caroundw5h, the link you included seems to just be an overview of stdio.h. Do you have a good source regarding linked lists in this manner?

    Thanks!
    Well there is always google. However this here seems to be a standard reply around here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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