Quick struct question

This is a discussion on Quick struct question within the C Programming forums, part of the General Programming Boards category; is this code valid? where intA is 0 - will this create a new structure called newStruct1? Code: struct appA ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    Quick struct question

    is this code valid?
    where intA is 0 - will this create a new structure called newStruct1?
    Code:
    struct appA newStruct[intA + 1];

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If intA is a constant, then it's valid to do that. It will not so much "create a new struct" as make an array with one element of such structs - that may well be what you meant, but creating a new struct [in the way I'd use those words] means to create a different struct, not create an instance of an existing struct.

    If intA is not a constant, then you are relying on compiler extensions to achieve this, and it's not standard compliant. If the compiler accepts it, and intA has a defined value at the point where this code occurs, it will work, but it won't necessarily compile with a different compiler.

    --
    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
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's valid in C99 constant or not, it will create an array with intA + 1 elements called newStruct. Not a variable called newStruct1

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks for the replies. More to the point, see my latest post...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. struct question
    By black_spot1984 in forum C Programming
    Replies: 5
    Last Post: 12-09-2006, 02:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. struct question...
    By Cheeze-It in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 06:28 AM

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