Question:
c++ sting problem output only one character?
2010-03-10 14:49:12 UTC
writting a program that uses the strcat in a seperate function but there was a problem with converting char to char* so i had a quick fix but now it will only display the first character

#include
#include
#include



char stringcat (char a[20], char b[20])
{
char o[40];
char c;
strcpy(o,a);
strcat(o,b);
c = *o;
return (c);
}



int main()
{
char first_string[20];
char second_string[20];
char output;

cout << "input two strings" << '\n';
cin.get(first_string,19);
cin.ignore(80,'\n');
cin.get(second_string,19);
cin.ignore(80,'\n');

output = stringcat(first_string,second_string);

cout << output;

system("pause");
return 0;
}
Five answers:
peteams
2010-03-10 14:59:06 UTC
The problem your trying to solve is to pass a string back from a C function. You construct a string in your function but you can't just return it because it's in an automatic variable allocated on the stack. Automatic variables disappear very quickly when their function returns.



Your solution is to return the first character, which as you say is inadequate.



There are three (or more) possible solutions, all of which change the return type of your function to char*:



1. Pass the string you wish to fill into the function from the called, i.e. make o a parameter (you can still return it as a char* for convenience).

2. Take a copy of o and return it, i.e. return strdup(o);. The caller will need to free the returned value.

3. Make o a static rather than an automatic, i.e. declare it as static char o[40]; then you can return it freely. This imposes some restrictions on the use of your function though.
?
2010-03-10 14:56:33 UTC
include

#include

#include







void stringcat (char *a, char *b, char *o)

{

strcpy(o,a);

strcat(o,b);

}







int main()

{

char first_string[20];

char second_string[20];

char output[40];



cout << "input two strings" << '\n';

cin.get(first_string,19);

cin.ignore(80,'\n');

cin.get(second_string,19);

cin.ignore(80,'\n');



stringcat(first_string,second_string, output);



cout << output;



system("pause");

return 0;

}
The Phlebob
2010-03-10 14:53:12 UTC
That's because output is declared a char, not a char*. But be VERY wary of returning a pointer to a variable local to a function. Such variables are assigned to places on the stack, which does not work well when pointers to them are returned.



Hope that helps.
?
2010-03-10 14:58:19 UTC
your code;

output is declared as

char output;

output = stringcat(first_string,second_string);

What I believe you wanted to do is:

char *output;

output = strcat(first_string, second_string);

This would work.
?
2016-10-20 05:37:33 UTC
comprehensive code #contain utilising namespace std; int important() { char str[20]="asdfdaertre"; int count extensive form=0; int i; for(i=0;str[i]!='0';i++) { if((str[i]=='a') || (str[i]=='e') || (str[i]=='i') || (str[i]=='o') || (str[i]=='u')) count extensive form++; } cout<<"form of vowels are "<


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...