array of typedef struct

This is a discussion on array of typedef struct within the C Programming forums, part of the General Programming Boards category; say I have: Code: typedef struct treeNode { .................. ....................... } TreeNode; and I want to create an array of ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    array of typedef struct

    say I have:

    Code:
    typedef struct treeNode   
       { ..................
         .......................
       } TreeNode;
    and I want to create an array of TreeNode, how do I do that?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We'll give you three guesses, first two don't count.

    No really. Go to your compiler, and try three things. If you don't get one of them right, then tell us what you tried and we'll go over it.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    We'll give you three guesses, first two don't count.
    Can I try ?

    Here's a secret: You can do this
    Code:
    typedef struct treeNode   
       { ..................
         .......................
       } *TreeNode;
    You do not need to specify the number of elements in *TreeNode when you declare it, but as with a *char, you need to malloc (and possibly realloc) enough space for it. Multiply by sizeof(*TreeNode).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Typedefing pointers can come back to haunt you later. Avoid it, is my advice.
    What is so hard about typing out a lone *?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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