ptr = LastFirstName(name); this doesn't really do anything. I mean, after that both ptr and name point to the same area in the memory... so it's pretty pointless (got it? 'pointless'... k, won't do that again.) to do that (the LastFirstName works on the memory pointed by name, which means that after the function ends name will hold the results.
It would be easier to ask for the name, and then last name, then just swap the addresses that the pointers are pointing to... but that would be no-fun and we wouldn't learn anything from that, would we? (I'm just saying that there's another option. Don't get me wrong)
Now let's get to the coding. Since returning pointer in this case doesn't make sense (unless you want to have 2 arrays, each with different order), so let's change the function's declaration into void LastFirstName(char *ptr).
Now we need to know where the name ends. We have to find the first occurence of a space (or any other character you want the two to be separated with), which is easily done with strchr(char*, int) function.
After that, since the name and last name doesn't need to be the same length we can't just swap their letters, it would be handy to just copy the name somewhere (another array), then override the name with the last name, copy the name right after that, and terminate with '\0'.
The code would look something like that (not tested, use with care):
void LastFirstName(char *ptr)
{
char name[41], ptr2(ptr);
const size_t nameLen(strchr(ptr, ' ')); // Change the space for whatever you want. btw, I used constructor here.
strncpy(name, ptr, nameLen); // Copy the name from "ptr" to "name"
ptr += nameLen+1; // Move pointer to the first character of the last name
/* We could use some pre-made functions to avoid doing the for loop below
but that's not the point in learning, is it? */
for(size_t i(strlen(ptr) - nameLen - 1); i>0; --i) // I also used constructor here
{
*ptr2 = *ptr;
++ptr2;
++ptr;
}
*ptr2 = ' ' // Again, you can change it to whatever you want
++ptr2; // it's better to do it that way - compiles into faster code
strncpy(ptr2, name, nameLen); // let's copy the name back into our array
ptr2 += nameLen; // Move the pointer to the end and append '\0'
*ptr2 = '\0'; /* We would have passed nameLen+1 previously (and now)
to get the null terminator copied within the strncpy, but it's better
that way - it helps you to understand what happens behind the
courtain. */
return; // unnecessary, but it's a good habit imho
}
/**
Since you pass a copy of pointer, and not a pointer itself, to a function,
you don't have to worry about getting it back to the position it was before.
*/
As you can see, I also made use of the strncpy(char*, char*, size_t) function.
Oh, and size_t is just a unsigned integer, used to express size in memory. It doesn't really differ from any other unsigned integer. It's just an alias.
And a little tip at the end: use C-style arrays (char array[]), wherever you can. Since their size is known at compile time the compiler can optimise it better. Not to mention that dynamic allocation (which I believe you will learn about next, that's what we use pointers for, mostly... left? I mean, right?) is just slower.
Hope I helped.