Hi i have written a standard library function 'strcat' on my own, even though the output seems ok, can someone please verify if what i'm doing is right? i get confused using pointers.

void mystrcat(char *a,char *b)
{
	while (*a++ != '\0');

	*a--;

	while (*a++ = *b++);

}

i'm assuming 'a' to be big enough to hold the concatenated value.

-Smith.S

while you are at it, can you also tell me why this is not working. i think i'm passing the address of the array and hence both the outputs should be same. but it seems to be getting passed by value. its supposed to trim any blanks at the starting of the string.

#include <iostream>

using namespace std;

void trimLeft(char *a)
{
	while (*a++ == '  ');

	*a--;

	cout << a << endl;

}

int main()
{
	char a[11] = "  smith";

	trimLeft(a);

	cout << a << endl;

};

output:
>
smith
smith (there's a space before this output line but here its getting aligned to the left border for some reason)

-Smith.S

trimleft() does nothing. After finding the last space you need to shift all remaining characters left. memmove() will do that for you, or you can do it yourself using two pointers.

What your trying to do with trimleft will only work with a reference to a pointer which is pointing at an array of chars, like this:

void trimLeft(char *&a)
{
  while (*a++ == ' ');
  a--;
}


int main() {
  char a[11] = "  smith";
  char *ptr = a;
  trimLeft(ptr);
  cout << ptr << endl;
}

You must realize that no data has been moved at all, however ptr is just pointing to a different part of the array you created to begin with. If you want to actually alter the array of characters, you will have to do something similar to what Ancient Dragon mentioned.

edit: I guess this thread should be moved to the C++ forum now ;)

Thanks for your replies...

"You must realize that no data has been moved at all, however ptr is just pointing to a different part of the array you created to begin with. If you want to actually alter the array of characters, you will have to do something similar to what Ancient Dragon mentioned."

I understand the part that no data has been moved. But what i fail to understand is that when i'm passing the address of array and incrementing the pointer why is it not having as the same effect as the code that you have given. Am i missing a level of indirection? Will it be possible for you to elaborate a little more.

I don't want to google it right away as it might give me the whole code which i don't want. Till then I'll writing the code using memmove to actually move the data.

-Smith. S

>> But what i fail to understand is that when i'm passing the address of array and incrementing the pointer why is it not having as the same effect as the code that you have given

Because you did not pass the address of the pointer -- all you did was pass the address of the start of the character array, which is not the same thing. Look closely at the & symbol in the trimLeft() function declaration -- that is a reference to a pointer.

>>Because you did not pass the address of the pointer -- all you did was pass the address of the start of the character array, which is not the same thing. Look closely at the & symbol in the trimLeft() function declaration -- that is a reference to a pointer.

ok i understand the point of passing the 'reference to a pointer', so when I'm passing the address of the start of the array and increment it its just shifting the pointer but not actually changing the start address and hence nothing is happening. Is my understanding right?

this is my code using memmove, this is working fine

void trimLeft(char *a)
{
	char *p = a;
	while (*p++ == ' ');
	*p--;
	size_t len = strlen(p);
	memmove((void *)a,(void *)p,len);
	*(a+len) = '\0';
}

Yes, your understanding was correct, the function was changing its 'copy' of the pointer and not the actual start address. (Changing the actual start address is probably a bad idea anyway.)

Your new trimleft would work as written.

A few things to think about:

This first might actually slow the code down a little, but I hate going past a corner and then backing up to turn:

// replace
while (*p++ == ' ');
*p--;
// with 
while (*p == ' ') 
    ++p;

Note that you could also use isspace(*p) if you #include <ctype.h> The second is a minor simplification:

// replace
memmove((void *)a,(void *)p,len);
*(a+len) = '\0';
// with
memmove((void *)a,(void *)p,len + 1);

The +1 makes memmove move the '\0' too.

Third, I'm pretty sure the (void *) casts are not needed, pointers will generally convert themselves to void * without help. (Getting back does require help.)

memmove(a, p, len + 1);

PS- When posting code in a source language, please use the appropriate language specific code tag:

For C++:
[code=c++] // your code here

[/code]

For C:
[code=c] /* your code here */

[/code]

Thanks for all those tips. So basically what i was doing was something like

char *p = "  smith";
char *a = p;

so even though i changed 'a' to point to the new location, 'p' never changed nor did the string.

Thanks everyone for being so patient. I have to write another 14 programs dealing with char *'s, arrays and I'm sure to pester you guys again :).

You have to be careful about string literals such as what you just posted above ^^^. String literals are (normally) stored in read-only memory so they can't be changed. What you posted originally is correect because the array isn't a string literal but a character array: char p[] = " smith";char*a = p; You can pass that to your trimLeft() function but not this: char *p = " smith";

yes you are right, thats one important point and i had read about that, thanks for pointing it out, my example above was not apt I realize that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.