Is it possible to declare an array without specifying the size of the array ?
I tried this
also tried thisCode:int i[];
but none of this seems to work..... Pls help me......Code:int i[]={};
Is it possible to declare an array without specifying the size of the array ?
I tried this
also tried thisCode:int i[];
but none of this seems to work..... Pls help me......Code:int i[]={};
No you can't.
You can make arrays with just the right size, by saying things like
Or you can omit the left-most size when specifying array parameters, likeCode:char message[] = "hello world";
Other than that, you need to know the size up front.Code:void foo ( char array[] );
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.
Thanks Salem.
Can you pls explain your code
Code:void foo ( char array[] );
Use pointer declaration and dynamic memory allocation
Code:int *i; // ... i = malloc(number_of_elements * sizeof(int)); // ... free(i);
No and yes...
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[] = {1, 2, 3, 4, 5}
You can't append or push values onto the array with the standard libraries. However, there are libraries that emulates that behavior.Code:int *i; [...] i = malloc(sizeof(int) * array_len);
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.
> 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.
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 asCode:void foo ( char array[] );
Code:void foo ( char *array );
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.
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.