?
Solved

strcat with variables?

Posted on 2003-02-19
4
Medium Priority
?
466 Views
Last Modified: 2008-03-06
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
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 200 total points
ID: 7981194
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 7981208
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 7981303
Thanking you! :) It compiled ok, but just brought up an error when it came to the strcat
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7984702
what error .. be more precise please
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question