0

I am trying to write 3 function: the first one: "read_comp" initialize a char pointer and assign it what function "readAndRemove" return. readAndRemove read line from the user and remove any spaces before the string and return a pointer to the string without the spaces in the start. then the "read_comp" print the string got by "readAndRemove" - the one without the spaces.

the last function - the one that i have problem with... function "findComplex": what i am trying this function to do is just to get char pointer and print the string that function got.

void read_comp(void)
{
    char *str = readAndRemove();
    printf("%s\n",str);
    findComplex(&str);
}

-------------

char * readAndRemove() /**function to read rest of input and remove first space**/
{
    char tempChar[30];
    fgets(tempChar,30,stdin);
    char* ptr = strtok(tempChar, " ");
    return ptr;
}

--------------
void findComplex(char* str)
{
    printf("in findComplex:%s\n",str);

}

(sorry if the start was irrelevant but i thought maybe there is problem with the way i am doing everything...)

so i tried to fix and change few things: change this: define char *str; as global parameter and chanege the function:

void read_comp(void)
{
    *str = readAndRemove();
    printf("%s\n",str);
    findComplex(str);
}

char * readAndRemove() /**function to read rest of input and remove first space**/
{
    char tempChar[30];
    fgets(tempChar,30,stdin);
    char* ptr = strtok(tempChar, " ");
    return ptr;
}


void findComplex(char* str)
{
    printf("%s\n",str);
    printf("in findComplex:%s\n",str);

}
Yuval
  • 1,721
  • 4
  • 16
  • 15
  • See [`strtok()` not working as expected](http://stackoverflow.com/questions/14030492/strtok-not-working-as-expected) for various discussions of the problems with `readAndRemove()`. – Jonathan Leffler Dec 25 '12 at 13:05

2 Answers2

0

The variable str in the read_comp function is already a pointer. Your use of the address-of operator & makes that a pointer to a pointer (i.e. type char **). Just make sure the findComplex function is prototyped before you call it, and don't use the address-of operator.

You have a larger problem though, and that is that the readAndRemove function returns a pointer to a local variable. Remember that local variables are stored on the stack, and that when a function returns that stack space is reclaimed to be reused by other function calls. Create the array in the read_comp function instead, and pass it together with its size to the readAndRemove function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • what do you mean by "findComplex function is prototyped"? Ok so i tried to change this: define char *str; as global parameter. – Yuval Dec 25 '12 at 10:04
  • and change the functions like this: void read_comp(void) { printf("you have entered: read_comp FUNCTION\n"); *str = readAndRemove(); printf("%s\n",str); findComplex(str); } – Yuval Dec 25 '12 at 10:06
  • @Yuval A function prototype is just a declaration of the function before it's used, so the compiler recognizes it. A prototype for the `findComplex` function would look like this: `void findComplex(char *);` This doesn't solve your biggest problem though. – Some programmer dude Dec 25 '12 at 10:07
  • didnt know the name was prototype :-) – Yuval Dec 25 '12 at 10:11
0

If you enable warnings in your compiler (and I'm saying if as in "please do this!") you would get a warning saying "returning pointer to local variable" or something like that for this:

char * readAndRemove() /**function to read rest of input and remove first space**/
{
    char tempChar[30];
    fgets(tempChar,30,stdin);
    char* ptr = strtok(tempChar, " ");
    return ptr;
}

You MUST not return pointers to local variables, because the space used by tempchar (which ptr will point into) is going to be reused by the next function when you return from this function - and most likely the next function will write something OTHER than your string into this memory.

The solution, I would suggest, is to move tempchar up to read_comp() [1], and pass the string to readAndRemove.

[1] Please try to decide whether you use "camelcase" or "_" names. Either your functions should be read_and_remove and read_comp or readAndRemove and readComp. I almost wrote it wrong because I expected to find the same style in both functions - this sort of thing can drive you mad when you later try to change something.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227