PDA

View Full Version : char *function(char *var) problem, please help


handana
12-11-03, 05:44 AM
this won't run but ok when compiled
what's wrong? please let me know

#include <stdio.h>
#include <string.h>

char *invok(char *inVar)
{
char *str;
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
return 0;
}

hyjacked
12-11-03, 08:31 AM
if it compiled your coding is legal, so my guess is it would have something to do with the pointers you are using. I don't have a compiler on this computer to test it out but shouldn't there be a & instead of a * in the function definition?

handana
12-11-03, 11:27 PM
if it compiled your coding is legal, so my guess is it would have something to do with the pointers you are using. I don't have a compiler on this computer to test it out but shouldn't there be a & instead of a * in the function definition?

For information this code run smoothly an no problem at all when I compile using Borland Turbo C 3 Dos Version, but not in GCC this code generate "Segmentation Fault".

handana
12-11-03, 11:53 PM
I have malloc inside "invok" function
so where I have to free the malloc?

does this code correct or wrong?

#include <stdio.h>
#include <string.h>

char *invok(char *inVar)
{
char *str;
str = (char*) malloc (30);
if (str==NULL) exit (1);
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
free (sts);
return 0;
}

hyjacked
12-12-03, 08:24 AM
I'm not sure where your problem is. I'll try to get on a computer with a compiler this weekend to see if I can figure it out.

Anyone else want to give this problem a shot in the meantime?

psyon
12-14-03, 11:44 AM
Two solutions, one with malloc, and one with static vars


#include <stdio.h>
#include <string.h>

char *invok(char *inVar) {
static char str[256];

memset(&str, 0, sizeof(str));
strcpy (str,"strings input =");
strcat (str,inVar);
return str;
}

int main () {
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
return 0;
}


and number two with malloc

#include <stdio.h>
#include <string.h>

char *invok(char *inVar) {
char *str = (char *)malloc(256);
strcpy (str,"strings input =");
strcat (str, inVar);
return str;
}

int main () {
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
free(sts);
return 0;
}


And finally you can replace a small piece of code

strcpy (str,"strings input =");
strcat (str, inVar);


with

sprintf(str, "string input = %s", inVar);

handana
12-17-03, 11:51 PM
this code allocate memory for *str
this code free variable *sts
so how to free the mem for *str we've allocate before?
does free(sts) also free *str?


#include <stdio.h>
#include <string.h>

char *invok(char *inVar) {
char *str = (char *)malloc(256); //<--- you've allocate mem for *str here
strcpy (str,"strings input =");
strcat (str, inVar);
return str;
}

int main () {
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
free(sts); //<-- free variable *sts
return 0;
}

albmons
12-30-03, 01:32 PM
this won't run but ok when compiled
what's wrong? please let me know

#include <stdio.h>
#include <string.h>

char *invok(char *inVar)
{
char *str;
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
return 0;
}


I just starting to learn this so let me now if this works for you

this compies and runs in MS c++ 6.0.
#include <stdio.h>
#include <string.h>

char* invok(char* inVar)
{
char strs[80]; // inserted

char* str = strs; // modified

strcpy (strs,"strings "); // modified
strcat (strs,"input = "); // modified
strcat (strs,inVar); // modified

printf( "String = %s\n", str ); // I put this in, can remove it

return str;
}

int main ()
{
char* str="test";
char* sts;

sts = invok(str);
puts(sts);

return 0;
}

digioz
12-31-03, 01:35 PM
You may also want to include the "#include <iostream.h>" in there. :)




this won't run but ok when compiled
what's wrong? please let me know

#include <stdio.h>
#include <string.h>

char *invok(char *inVar)
{
char *str;
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
return 0;
}

psyon
01-03-04, 06:31 AM
I would suggest doing alot of reading on pointers


this code allocate memory for *str
this code free variable *sts
so how to free the mem for *str we've allocate before?
does free(sts) also free *str?


Since invok returns the pointer to allocated memory, sts then contains that memory address. When you call free(sts) it frees the memory that was allocated in invok()

Lobo Lunar
01-10-04, 10:58 PM
Hello!
I have this, maybe I'll help you

#include <stdio.h>
#include <string.h>

char* invok(char inVar[260])
{
static char str[260];
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char* sts = invok("test");
puts(sts);
return 0;
}

<?RI?>
02-07-04, 11:11 PM
this won't run but ok when compiled
what's wrong? please let me know

#include <stdio.h>
#include <string.h>

char *invok(char *inVar)
{
char *str;
strcpy (str,"strings ");
strcat (str,"input = ");
strcat (str,inVar);
return str;
}

int main ()
{
char *str="test";
char *sts;
sts=invok(str);
puts(sts);
return 0;
}

THIS IS WORKING!!!

I tried it on Borland C++ 3.1. ALL RIGHT!!!

Answer: strings input = test