0

Can I create an array with no specific size or intializer? I'm trying to make an array of 26 english character but I don't want to just declare 26 slots right from the beginning

int main(void)
{
    int i = 0;
    char array[];
    for(char c = 'a'; c <= 'z'; c++)
    {
        i++;
        array[i] = c;
        printf("array %i = %c\n", i, c);
    }
}

1 Answer 1

1

C requires you to declare the size of an array explicitly unless you initialize it within the declaration. Example: int array[] ; might not always work. Some compilers might allow it, but it is recommended not to use it. Better use dynamic memory allocation for cases where size of array is known at runtime only. But, int array[26]; will work. It creates an array of 26 integers which are in initialized. So will int array[] = {0,0,0}; The last one will initialize an integer array of size 3 containing all zeroes.

1
  • Oh that makes sense, I will make sure to use int array[] = {0,0,0} from now on. Thansk
    – Huynq
    Apr 28, 2017 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .