• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

strcat with variables?

I've got 2 variables of type char*. But I'm wanting to join one onto the other:

function (char*A, char*B)
{
   strcat(A,B);
}

This doesn't work because B isn't a constant string. Can anybody help?

Thanks
Uni
0
Unimatrix_001
Asked:
Unimatrix_001
  • 3
1 Solution
 
akshayxxCommented:
whats the error did u get.. did u get the compilation error .. or did u see unexpected result( the concatanation didnt happen)..


following works with me..
#include <stdio.h>
#include <string.h>

void ur_function(char *a,char *b){
strcat(a,b);
}
main(){
char *a;
char *b=".suffix";
//malloc and fillup values in a and b

a=(char*)malloc(32); // some good enough size to hold both prefix and suffix
strcpy(a,"prefix");
ur_function(a,b);
printf("%s %s\n",a,b);
}
0
 
akshayxxCommented:
this also works .. if u say ur code doesnt work for non constant char *B

char *a;
char *b;
//malloc and fillup values in a and b

a=(char*)malloc(32); // some good enough size to hold both prefix and suffix
b=(char*)malloc(10);
strcpy(a,"prefix");
strcpy(b,".suffix");
ur_function(a,b);
printf("%s %s\n",a,b);
0
 
Unimatrix_001Author Commented:
Thanking you! :) It compiled ok, but just brought up an error when it came to the strcat
0
 
akshayxxCommented:
what error .. be more precise please
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now