Thread: Malloc for an Array of Structs

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Question Malloc for an Array of Structs

    Hey all,

    I was trying to malloc an array of structs and i always gets an error for incompatible types
    Here's part of my code:
    Code:
    #define MAX_COMMANDS 50
    
    typedef struct {
    	SpecType Type;
    	RangeSpec Range;
    	char Command;
    	String Text;
    } EditCommandType;
    
    EditCommandType CArray[MAX_COMMANDS];
    
    CArray = (EditCommandType *)Malloc(50 * sizeof(EditCommandType));
    What am i doing wrong??
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have defined it as an array, you already have the memory allocated. You can do arrays or malloc, never both. And malloc starts with a lowercase m.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    i have it with an uppercase M because I have my own function.
    The problem is that when I try to access an array location i.e. CArray[1] it gives me an error that it cannot access the memory location

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    malloc or Malloc -- if you define EditCommandType CArray[50], you have the memory and must not try to allocate more.

    Also, CArray[1] is a struct, so you should deal with CArray[1].Type, CArray[1].Range, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  2. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  3. Inserting new member into array of structs
    By cathym in forum C Programming
    Replies: 6
    Last Post: 04-17-2005, 07:10 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array of structs
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 04-18-2002, 01:51 PM