0

Hi I am trying to get a space for storing a number of words. My plan is to use a two-dimension array, which stores a string. It looks like this:

pBuffer[0]---------->myBuffer[0][0] myBuffer[0][1]...
pBuffer[1]---------->myBuffer[1][0] myBuffer[1][1]...
..

However, it gets error and I am struggling to debug this. What's the problem?

1. Declaration

char myBuffer[255][255];
char* pBuffer[255];

2. Usage

char* pBuffer[i] = &myBuffer[i][0];
pStream->cmdArray[i].u.word = &pBuffer[i];
strcpy(*pStream->cmdArray[i].u.word, infix_string);
i++;

3. Error & Warning

error: variable-sized object may not be initialized
  char* pBuffer[i] = &myBuffer[i][0];
  ^
warning: unused variable 'pBuffer' [-Wunused-variable]
   char* pBuffer[255];
1
  • put entire code, please update your post. Oct 5, 2013 at 20:16

2 Answers 2

2
char* pBuffer[i] = &myBuffer[i][0];

Remove the leading "char*", the compiler thinks you're trying to declare a new variable called pBuffer which "shadows" the original definition of pbuffer.

char* pBuffer[255]; // declares pbuffer as an array of 255 char* pointers
char* pBuffer[i];   // illegal and, if it worked, would be a shadow declaration.
1
  • If you're using a faintly modern version of C (not the 23+ year old version but the 13+ year old version — C99, or the more recent C11), then the char *pBuffer[i]; declaration is a valid VLA (variable length array) definition, assuming i is an integer variable (or a name for an integer constant, but then it isn't VLA any more). But you can't provide any initializer for a VLA, as the compiler error message indicates. Oct 5, 2013 at 21:53
1

Because you wrote something weird. It should just be pBuffer[i] = &myBuffer[i][0];, in a loop of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.