0

I am writing a C program, but I get the following error for the pvowels pointer:

[cquery] initializer element is not a compile-time constant

Here is the code:

int n = 5;
char *pvowels = (char *) malloc(n * sizeof(char));

pvowels[0] = 'A';
pvowels[1] = 'E';
*(pvowels + 2) = 'I';
pvowels[3] = 'O';
*(pvowels + 4) = 'U';

for(int i = 0; i < n; i++) {
    printf("%c ", pvowels[i]);
}

printf("\n");

free(pvowels);
Angelina Tsuboi
  • 196
  • 1
  • 2
  • 14

1 Answers1

2

In C, all executable code must reside in a function. Only variable declarations with a constant initializer may exist outside of a function.

Put this code inside of a main function, which is the starting point for a C program. Also, you'll need the relevant #include directives for the functions you're using.

#include <stdio.h>      // for printf
#include <stdlib.h>     // for malloc,free

int main()
{
    // you code here
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273