1

I have to take a phrase from the user, and print it out in an inverted triangle with spaces in between. My program takes the phrase, stores it into an inputBuffer (array of char), and then creates a new array double the size of string. It fills up the first half with spaces, and the second half with the string itself. I want to print out the strLength char from the new array strLength times, just by shifting the range of strLength to (strLength*2-1) by 1 each time to the left. This ensures that in the first iteation, only the whole string is printed, second time one space at the beginning and one char at the end is not printed, as so on.
Currently I am getting an error that even through strLength is an int variable, when I use it to create the new array it's apparently NOT a constant value.

int main(void) {

char inputBuffer[256];
char *pointer = inputBuffer;
char *temp = pointer;
int strLength = 0;

printf("enter your word: ");
scanf("%s", pointer);

//Calculate string length
while (*temp++) strLength++;

//Create an array double the size, first half for white spaces, and second half for the phrase.
char inputString[strLength * 2];
// ERROR: above expression inside the index must be a constant value.
int i, j;

//First half of array is number of spaces == number of char in phrase

for (i = 0; i < strLength; i++) {
    inputString[i] = ' ';
}

//Reinitialize temp to use instead of pointer & put the string in the second half of inputString[]

temp = pointer;
for (j = 0; j < strLength; j++) {
    inputString[i++] = *temp++;
}

//Just print the strLength indexes of inputStrng[] starting from half to end, and keep shifting the range by 1 position to the left.

for (i = strLength; i < (strLength * 2); i--) {
    for (j = 0; j < strLength; j++) {
        putchar(inputString[i + j]);
        putchar(' ');
    }
    putchar('\n');
}

return 0;
}
ScaVenGerS
  • 61
  • 1
  • 10
  • Can you please post the full error message? – HeyYO Nov 15 '15 at 13:04
  • It seems that your C compiler doesn't follow the latest C standards. In older dialects of C, no variable (not even a const-declared one) can be used for array size. It has to be an integer literal, or an expression only containing integer literals. (Preprocessor macros are expanded first, so this applies to what the macro expands to.) – Thomas Padron-McCarthy Nov 15 '15 at 13:07
  • @HeyYO The comment with ERROR is the full error i am getting - "expression must have a constant value" – ScaVenGerS Nov 15 '15 at 13:09
  • @ThomasPadron-McCarthy so how would I create an array double the size of the array? BTW I'm using Visual Studio 2015 community edition, I believe it is using gcc as it's compiler but there's a god chance I'm wrong. – ScaVenGerS Nov 15 '15 at 13:10
  • @ScaVenGerS: `malloc` – Thomas Padron-McCarthy Nov 15 '15 at 13:11
  • As you say, it's a variable -- it can vary. Did you try "const int strLength;"? Also, its a bad habit to start calling anything a "string" in C if there's no null terminator character. The compiler doesn't care about the content of identifiers, but they can cause a lot of errors through human confusion. – 15ee8f99-57ff-4f92-890c-b56153 Nov 15 '15 at 13:12
  • @ThomasPadron-McCarthy: But his variable is not `const`-declared one in the first place ... nor does the title have anything to do with the question. – Tim Čas Nov 15 '15 at 13:32

1 Answers1

0

The index variable your using to create inputString is infact not a constant, it's a variable..

If you want to create an array of a variable size, you have to use malloc.. You have to replace the line char inputString[strLength * 2]; with a malloc statement..

See this answer

So.. something like this:

char * inputString = malloc( sizeof(char) * ( (strLength * 2) + 1 ) );
Community
  • 1
  • 1
Wiingaard
  • 4,150
  • 4
  • 35
  • 67