Shx
Shx

Reputation: 47

Strange output when trying to return char pointer

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

Upvotes: 4

Views: 521

Answers (2)

Vishal Yarabandi
Vishal Yarabandi

Reputation: 457

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.

Upvotes: 0

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27271

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

Upvotes: 2

Related Questions