Thread: Array size problem in C

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Unhappy Array size problem in C

    Is it possible to declare an array without specifying the size of the array ?
    I tried this
    Code:
    int i[];
    also tried this
    Code:
    int i[]={};
    but none of this seems to work..... Pls help me......

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No you can't.

    You can make arrays with just the right size, by saying things like
    Code:
    char message[] = "hello world";
    Or you can omit the left-most size when specifying array parameters, like
    Code:
    void foo ( char array[] );
    Other than that, you need to know the size up front.

    Or tell us the problem you're trying to solve.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Thanks Salem.
    Can you pls explain your code
    Code:
    void foo ( char array[] );

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Use pointer declaration and dynamic memory allocation
    Code:
    int *i;
    // ...
    i = malloc(number_of_elements * sizeof(int));
    // ...
    free(i);

  5. #5
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    No and yes...
    Code:
    int i[] = {1, 2, 3, 4, 5}
    If the array values are unknown you'll have to allocate space for the array later, when you know how much space is needed.
    Code:
    int *i;
    
    [...]
    
    i = malloc(sizeof(int) * array_len);
    You can't append or push values onto the array with the standard libraries. However, there are libraries that emulates that behavior.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It depends on whether your compiler supports C89 (the 1989 C standard) or C99 (the 1999 C standard).

    If you are using C99 (the 1999 C standard), then look up VLA (variable length array). Note that a VLA is not an array that magically resizes itself when you try to access an element - you have to explicitly size the array when you know the desired size.

    If you are using C89 (the 1989 C standard) then what you want is not valid. You can work around that by declaring i as a pointer, and using dynamic memory allocation (malloc(), etc.

    If using your second method, you need to initialize with some number of elements.
    Last edited by grumpy; 06-09-2011 at 05:36 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Thanks Salem.
    > Can you pls explain your code
    Read your book / course notes / tutorials.
    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.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    void foo ( char array[] );
    The function foo accepts an array of characters, which actually degrades to a char *. If you looked at this in the debugger you would see it is being treated as
    Code:
    void foo ( char *array );

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    > Thanks Salem.
    > Can you pls explain your code
    Read your book / course notes / tutorials.
    I thought you might be kind enough to share your knowledge but am obviously wrong..... Sorry for asking you to explain

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It is your responsibility to learn things. It is your school/tutor to teach you these things.
    If they don't, you have to ask them.
    Even if you are not student, you could still read online tutorial or books.
    Forum is not a place to teach you basic C programming skill, we are here to help you solve your problem.
    The fundamental thing like this should be covered by yourself using your own effort.
    Why don't you see recommended books and read them up.
    If you don't understand, come here back and ask what you don't understand.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    We're not here to spoon-feed you entire lessons in C.

    Especially when you bounce your reply back only 5 minutes after I posted my reply.

    That just says "lazy" to me - there is no way you could possibly have followed up with some research of your own.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  2. How do I get the size of an array from a pointer to the array?
    By Programmer_P in forum C++ Programming
    Replies: 31
    Last Post: 06-03-2010, 04:13 PM
  3. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  4. array size problem
    By benhaldor in forum C Programming
    Replies: 2
    Last Post: 09-14-2007, 07:44 PM
  5. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM

Tags for this Thread