0
string result="CCY 1.2597 Down 0.0021(0.16%) 14:32 SGT [44]";
char* token;
char* buffer[result.length() + 1];  //Space for '\0'

strcpy(buffer, result.c_str());
buffer[result.length()] = '\0';     //insert '\0'
token = strtok(buffer, " ");
while (token != NULL) {
  /* work with token */
  token = strtok(NULL, " ");
}

I not sure why the above code got error, what is wrong with my code

main.cpp:51:30: error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
main.cpp:53:27: error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
make: *** [main.o] Error 1

BUILD FAILED (exit value 2, total time: 893ms)
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
user1548465
  • 65
  • 1
  • 6

4 Answers4

4

You have two problems with your code: Firstly, the variable type for buffer is wrong, which is the error the compiler finds. Secondly, you are (probably unknowingly) deviating from the C++ standard by using the non-standard variable length array (VLA) extension of the gcc. This feature is standard in C from C99 on, but not in C++.

For the variable declaration, the following line is wrong:

char* buffer[result.length() + 1];

This line allocates an array of pointers to characters on the stack. However, strcpy expects a single pointer to a character vector. While an array type (like char[]) decays into a pointer (char*), an array of pointers to characters (char*[]) decays into char**, which is one pointer too many. Declare instead:

char buffer[result.length() + 1];

Now, this will still compile only on gcc. To make your code compatible, you'll have to make the memory allocation dynamic, e.g. by using:

char *buffer = new char[result.length() + 1];

and then performing

delete[] buffer;

at the end of your routine. You should use smart pointers (std::unique_ptr) to make your life easier here.

thiton
  • 35,651
  • 4
  • 70
  • 100
1

Your definition

char* buffer[result.length() + 1]; 

is wrong.

char* buffer = new char[result.length() + 1];

You have to initialize the c-string with dynamic memory allocation.

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
  • Is there a reason to use heap instead of stack allocation? I suspect the different allocation will confuse the OP and divert from the real type issue. – thiton Jul 24 '12 at 11:08
  • 2
    @thiton Does C++ even support VLAs? I thought that was a C99 thing. – FatalError Jul 24 '12 at 11:10
  • @thiton : How the compiler know how many bytes to be reserved in stack at compile time? – PermanentGuest Jul 24 '12 at 11:11
  • Yes, VLAs are nonstandard in C++ and a gcc extension => dynamic allocation is preferable here. Could you explain that in your answer, or may I update it accordingly? The issue is real, but needs elaboration. – thiton Jul 24 '12 at 11:12
  • @thiton : I'm surprised that it such an extension is available. How does the compiler knows about the size? – PermanentGuest Jul 24 '12 at 11:14
  • 1
    @PermanentGuest: It doesn't, and it needn't. At runtime, the stack top pointer is simply modified as needed for the dynamic array size. – thiton Jul 24 '12 at 11:16
  • ok, interesting... new learning for the day... anyway, my msvc2010 compiler doesn't allow that.. – PermanentGuest Jul 24 '12 at 11:18
  • Yes, that is exactly the point here. I've modified my answer to include this information. – thiton Jul 24 '12 at 11:19
0

The buffer should not be char*, but just char.

It will contain the characters, not point to them.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Declaration off buffer is wrong. Get rid of the '*'. It should be

char buffer[result.length() + 1]; 
Wolfgang Ziegler
  • 1,675
  • 11
  • 23