Error in the returning of a pointer char function

Hello, I am supposed to make the strcat_s function on my own. This is the code I have made until now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
char *strcat(char *, const char *);
int main()
{
	char a[100];
	cout << "Enter First String: ";
	cin.getline(a, 100);
	char b[100];
	cout << "Enter Second String: ";
	cin.getline(b, 100);
	a = strcat(a, b);
	system("pause");
	return 0;
}
char *strcat(char *a, const char *b)
{
	char c[100];
	int i = 0;
	for (i; i < 100; i++)
	{
		if (a[i] == '\0')
			break;
		c[i] = a[i];
	}
	for (i; i < 100; i++)
	{
		if (b[i] == '\0')
			break;
		c[i] = b[i];
	}
	return c;
}


I am not quite sure if this will work fine because I am unable to test it due to one error, in the line,
 
	a = strcat(a, b);

It is saying that it can't assign the function to "a" array... which I am unable to understand. Because my function is returning an address of character array and that address should be able to store in a as it is an array (which is a pointer itself).
So, what is it here that I am getting wrong and what should I do to fix it?
Thank you!
1
2
char a[100];
a = strcat(a, b);

How do you intend to assign a pointer to an array? (All we know is that the function returns a pointer.)
You would have to explicitly copy values from one array to an another.

Test:
1
2
3
4
5
6
7
int foo[5];
int* bar = nullptr;
foo = bar; // error;

// EDIT: error for arrays too:
int gaz [42];
foo = gaz; // error 



1
2
char c[100];
return c;

You have an array inside the function. It's memory will be deallocated at the end of the function.
You will return the address within stack that no longer has a valid array.
That is a fatal mistake.

The second loop:
Lets take "Hello" + "foobar".
The first loop ends because a[5] is null.
Then you test b[5]. The 'r' is not null and you copy:
c[5] = b[5]
Was your intention to skip the "fooba"? What if b had been "foo"?


If you do have two 99-char strings, how could they fit into one 99-char text (the 100th char is for null)?


Read the description of strcat_s:
https://en.cppreference.com/w/c/string/byte/strcat
Last edited on
to get started, ignore the return value.

char *strcat(char *a, const char *b)
{
... copy b onto the end of a here, including b's terminal zero char.
overwrite a's terminal zero as the starting point.
}


once you have that working, you can fix the return value.
Last edited on
Thanks for the replies, guys! :)
I got it working by this code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
char *strcats(char *, const char *);
int main()
{
	char a[100];
	cout << "Enter First String: ";
	cin.getline(a, 100);
	char b[100];
	cout << "Enter Second String: ";
	cin.getline(b, 100);
	char *z = new char[100];
	z = strcats(a, b);
	cout << endl << z << endl; 
	delete[]z; 
	system("pause");
	return 0;
}
char *strcats(char *a, const char *b)
{
	char *p = &a[0]; 
	while (*p != '\0')
		p++; 
	for (int i = 0; b[i] != '\0'; i++)
	{
		*p = b[i];
		p++; 
	}
	return a;
}


(Just one slight problem, which is this that it also prints all the garbage values but that's easy to fix and will do it now... Otherwise, working perfectly!)
Thank you :)
Topic archived. No new replies allowed.