Thread: typedef structure help

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    typedef structure help

    Are these two statements equivalent??

    Code:
    struct coords
    {
    	double x, y , z;
    };
    typedef coords points[50];
    Code:
    typedef struct 
    {
    	double x, y , z;
    }coords;
    
    coords points[50];

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    Yes they are.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Are these two statements equivalent??
    No.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Salem View Post
    > Are these two statements equivalent??
    No.
    I am thinking that in the first statement, Points[50] is a structure tag, whilst in the second statement Points[50] is a variable.
    Is this correct?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well in C, you would need
    typedef struct coords points[50];

    The new typename in this is just points. It being a synonym for an array of 50 coords.

    With this, you could just have:
    points p;

    and then go on to say:
    p[0].x = 0;

    Whilst legal code, typedef'ing arrays is like typedef'ing pointers(*) - it seems to obscure more than it enhances.


    (*)
    The exception is typedef'ing pointers to functions, which should be typedef'ed all the time IMO.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Salem View Post
    Well in C, you would need
    typedef struct coords points[50];

    The new typename in this is just points. It being a synonym for an array of 50 coords.

    With this, you could just have:
    points p;

    and then go on to say:
    p[0].x = 0;

    Whilst legal code, typedef'ing arrays is like typedef'ing pointers(*) - it seems to obscure more than it enhances.


    (*)
    The exception is typedef'ing pointers to functions, which should be typedef'ed all the time IMO.
    Can I go on to say that 'p' can at most have 50 elements?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Structure - Typedef accessing problem
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 08-11-2006, 11:52 PM
  4. Structure and typedef
    By Roaring_Tiger in forum C Programming
    Replies: 6
    Last Post: 03-12-2003, 08:27 PM
  5. structure & typedef
    By beely in forum C Programming
    Replies: 9
    Last Post: 10-30-2002, 08:48 PM