-1
char ParseCmd(char *buf,int len)
{
     char *p;
     p = strtok(buf," ");
     return *p;
}

Why does this function only return first symbol in a whole buffer? If I set buffer to a "fsa rew qwe" it returns only "f" instead of the expected "fsa".

"mˣ*" - that is now im getting. why ?

char dum = *InstList->Lines->GetText(); LoadLibrary("SyntaxP.dll"); char *dum1 = ParseCmd(&dum,32); InstList->Lines->Add(dum1);

Johan
  • 74,508
  • 24
  • 191
  • 319
Hakon89
  • 1,011
  • 2
  • 9
  • 17
  • Why are you using C strings and `std::strtok()` in C++ when you so obviously have no idea about how they work? Use `std::string`, that is much easier for beginners. And get yourself [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ASAP. – sbi Oct 28 '11 at 13:51

3 Answers3

2

Because your return type is char which represents a character and you dereference the pointer returned by strtok().

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

Because you are returning a char value, which means only the first character of the string pointed by pointer p.You should return a char * from your function.

Your function should have the prototype:

char* ParseCmd(char *buf,int len);
^^^^^

Online Demo:

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

char* ParseCmd(char *buf,int len)
{
     char *p;
     p = strtok(buf," ");
     char *ptr = (char *)malloc(strlen(p)+1);
     strncpy(ptr,p,strlen(p));
     return ptr;
}

int main()
{
    char array[]="fsa rew qwe";
    char* ret = ParseCmd(array,11);
    printf("[%s]",ret);

    /*If You Forget this,You cause a Memory Leak*/    
    free(ret);

    return 0;
}

Output:

[fsa]

Disclaimer: I have not really used any C++ in the code because since You are using strtok and char * instead of string I believe the Q is more C than C++.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • and now im getting "mÀ£*" that string, but the hell? what am i doing wrong again? – Hakon89 Oct 28 '11 at 12:24
  • @Hakon89: Please post your program. – Alok Save Oct 28 '11 at 12:28
  • `char dum = *InstList->Lines->GetText(); char *dum1 = ParseCmd(&dum,32); InstList->Lines->Add(dum1);` – Hakon89 Oct 28 '11 at 12:29
  • dum should also be a char*, not a char. char is a character, char* is a pointer to a (set of) characters - ie a string. What you are currently doing is getting the text, then copying the first character into dum. You are then passing the address of dum as your string, but it is not a string, it is a character. ParseCmd() expects a string, so it will carry on reading through memory until it hits a null, which is why you are getting garbage. – GazTheDestroyer Oct 28 '11 at 12:33
  • @Hakon89: Added a sample program. – Alok Save Oct 28 '11 at 12:34
  • Thank you! but what if i want to get a string from a TMemo ? – Hakon89 Oct 28 '11 at 12:44
  • @Hakon89: What is an `TMemo`? Please note that Usually We don't *write* code for you but just provide guidelines on how to solve your problems.I added the code here because I thought you were confused about the usage,You should read a good book or see the [strtok](http://www.cplusplus.com/reference/clibrary/cstring/strtok/) documentation to figure out your problems and only ask when you are stuck at a specific problem.And no offence but this is how it works here. – Alok Save Oct 28 '11 at 12:48
0

Like any C-style string, p is actually a character array. If you dereference it, you get a character. Have your ParseCmd return p instead of return *p.

robert
  • 33,242
  • 8
  • 53
  • 74