Like Tree3Likes
  • 2 Post By Salem
  • 1 Post By Salem

typedef structure help

This is a discussion on typedef structure help within the C Programming forums, part of the General Programming Boards category; Are these two statements equivalent?? Code: struct coords { double x, y , z; }; typedef coords points[50]; Code: typedef ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > Are these two statements equivalent??
    No.
    stahta01 and bos1234 like this.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    stahta01 likes this.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    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, 04: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, 07:27 PM
  5. structure & typedef
    By beely in forum C Programming
    Replies: 9
    Last Post: 10-30-2002, 07:48 PM

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