Copy pointer to pointer in function - C

Pages: 12
And for a simpler version using shift rather than a temporary buffer, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int insert(char* str1, const char* str2, size_t n)
{
	size_t l1 = lenstr(str1);
	size_t l2 = lenstr(str2);

	if (l2 == 0 || l1 == 0 || l1 < n)
		return -1;

	char* stbuf = str1 + l1;
	char* endb = stbuf + l2;

	for (size_t i = 0; i < n; ++i)
		*endb-- = *stbuf--;

	for (; *str2; *++stbuf = *str2++);

	return 1;
}

There are 2 ways to say don't change the signature:
1) don't change it at all, period, hard lock.
2) it needs to work with older / existing code, if you modify it you can't break anything.

1, there isnt much to do about.
2, for example, if you had
void foo(int x);
you can make it
void foo(int x, int y=42); //calling it with 1 parameter still works for old code. if y is 42 you know its an old call and can do something special or whatever.

in that regard,
int insert(char*& str1, char* str2, size_t n)
this can be called by the existing code without problems, but I DID change it. This small change lets you change the *pointer* str1 (delete and replace it). Whether this is useful to you now or not... probably not. And if you have arrays passed in as well as raw pointers, it would not go well if you tried to change an array's pointer.
Last edited on
> in that regard,
> int insert(char*& str1, char* str2, size_t n)
> this can be called by the existing code without problems

No. It may break existing code.
Topic archived. No new replies allowed.
Pages: 12