Reputation: 1010
I haven't programmed any C for a while and in preparation to take on a side project I decided to write up a few basic functions to get my brain back into thinking in C. One of them I attempted is basic char array copy function:
char* mysteryFuncOne(char* in)
{
char* returnVal = malloc(sizeof(char) * strlen(in));
int i = 0;
while(*(returnVal+ i)= *(in+ i))
{
i++;
}
return returnVal;
}
What I have above works, but I'm attempting to further jolt my memory and I also thought this could work:
char* mysteryFuncOne(char* in)
{
char* returnVal = malloc(sizeof(char) * strlen(in));
while(*(returnVal++)= *(in++) ) // I changed this loop
{
}
return returnVal;
}
Basically I changed the conditions of the while loop. I thought these were equivalent forms? The first version works just fine, the second version however returns me an empty result. I feel like I'm missing something obvious here but I just can't spot it.
Anyone want to show me the obvious thing I'm missing?
Upvotes: 0
Views: 425
Reputation: 99144
In the second version, you return a pointer to the end of the new array.
Upvotes: 4