4

Edited: I deleted memory allocation declarations and changed strlen(bufferPointer) to strlen(inputLine). This seems to get rid of the weird symbols in my output.

I'm trying to write a method to remove the first word in a string, and return char pointer of the word removed. Because this word is removed, the size of the string should be reduced.

I'm encountering a strange output and I'm not sure why.

I'm fairly new to C and just beginning to familiarize myself with the idea of pointers, so any help would be appreciated!

//global variables
char inputLine[] = "Hello there, my name is bob"; 
char *bufferPointer = inputLine;
char *nextWord();

main (){
    printf("%s\n", nextWord());
}

char *nextWord(){
    //calling a method that returns the number of words bufferPointer holds
    int numOfWords = nwords(bufferPointer);
    char *tmp2;

    //Allocate memory to newArray
    char *newArray = malloc(sizeof(char)*strlen(bufferPointer));

    //create backup array
    char backup[strlen(bufferPointer)];
    strncpy(backup, bufferPointer, strlen(bufferPointer));
    backup[strlen(bufferPointer)] = '\0';

    //assign newArray pointer to backup array
    newArray = backup;

    //allocate memory to token (returned variable)
    char *token = malloc(sizeof(char)*strlen(bufferPointer));
    token = strtok(newArray, " ");

    char *tmp = strchr(bufferPointer, ' ');
    //move pointer to next word
    if (tmp != NULL){
        tmp2 = tmp;
        bufferPointer = tmp +1;
    }
   return token;
}

Old output is:

there,
my
?²p
??
?²p?
?²p?

New output is:

there,
my
name
is
bob
6
  • Where is nextToken() defined? Commented Mar 28, 2016 at 3:58
  • 1
    nextWord, nextToken?? Which is it? Commented Mar 28, 2016 at 3:58
  • Sorry guys, I meant nextWord. I have edited the code. Commented Mar 28, 2016 at 4:00
  • nwords() is counting white spaces? One suggestion - read carefully the description of strtok (cplusplus.com/reference/cstring/strtok) - pay attention that after strtok your string is "dirty" Commented Mar 28, 2016 at 4:02
  • Interesting that backup array can be created on stack with a modifiable size (strlen...). Is this some gcc extension? Commented Mar 28, 2016 at 5:48

2 Answers 2

2

strlen() only gives you the number of characters, excluding the null character. You also need to allocate memory for the null character.

Sign up to request clarification or add additional context in comments.

5 Comments

I added a +1 to malloc(sizeof(char)*strlen(bufferPointer)) and it works! Thanks a lot!
@Shx: Please help yourself and don't use strlen(bufferPointer) on multiple, consecutive lines. Use: int L = strlen(bufferPointer); and then use L instead. Your code will get a readability boost. Also sizeof(char) = 1 is defined for eternity, I would guess.
@ikrabbe C99, section 6.5.3.4: "When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1."
well C99 is not eternity, but strlen(char) should be ;)
@ikrabbe I've modified my code so that I no longer have to consecutively call strlen(bufferPointer) :), but I will definitely take the feedback into account for the future! Thanks!
0

You are returning token. Instead you are supposed to return bufferPointer.

return bufferPointer. 

And just a point out, there are several memory leaks in your code.

On your request, following are the points.

Memory leaks in the above program are highlighted below.

//Allocate memory to newArray
char *newArray = malloc(sizeof(char)*strlen(bufferPointer));
// A memory address is given to newArray here which is
// over-written in following statement. So the above allocated
// memeory is a leak.
newArray = backup;

//allocate memory to token (returned variable)
char *token = malloc(sizeof(char)*strlen(bufferPointer));
token = strtok(newArray, " ");
//There is no need to allocate memory to hold an address of a string.
// only a character pointer is enough. So, the above allocated memory
// is again a leak. 

And there are two extra variables defined which are not necessary.

1) The variable bufferPointer can be replaced with inputLine since name of a character array is also a pointer in C.

2) you are not using numOfWords anywhere at all.

1 Comment

I am returning token because I want to return the single word that was removed. bufferpointer will return the whole string minus the removed word. Please let me know if I've misinterpreted your comment. Furthermore, I would really appreciate it if you could perhaps point out the general area where the memory leaks could occur so I could take a look and try to debug.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.