Thread: field `chars' has incomplete type

  1. #1
    Unregistered
    Guest

    Exclamation field `chars' has incomplete type

    Need to get over this hump to see how the rest of my codes are going. Here's a snippet of myString.h

    struct String
    {
    char chars[];
    };

    typedef struct String * String;

    I decided to represent a String as an array of char. But whenever I try to compile mystring.c (which includes "mystring.h") the message appears:

    mystring.h:9: field `chars' has incomplete type

    Please reply soon.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: field `chars' has incomplete type

    Originally posted by Unregistered

    struct String
    {
    char chars[];
    };
    This should be declared in one of these ways:

    char chars[100]; /* or whatever size you want */
    char *chars;

    The first will give you 100 bytes of memory automatically, the second will give you a pointer, for which you will need to malloc memory to hold your string. As you're beginning, start with option 1, its easier to get to grips with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you declare an array, then you must give it a size immediately. This can be done with a constant value such as in

    char chars[10];

    Or with an initialization list:

    char chars[] = "Some string";

    If you want to set the size later, then you must declare chars as a pointer to char and allocate the memory when you need it:
    Code:
    char *chars;
    .
    .
    .
    if ( ( chars = malloc ( n * sizeof *chars ) ) != NULL ) {
      /* Work with the array */
      free ( chars );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program error
    By cutelucks in forum C Programming
    Replies: 14
    Last Post: 12-21-2007, 11:12 PM
  2. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM