473,883 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcpy and strcat problem

JC
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"
Nov 13 '05
23 11632
#include<stdio. h>
#include<string .h>
int main(void)
{
char a[3] = "ab";
char b[3] = "cd";
char c[5] = " ";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

See your problem
Nov 13 '05 #21


amanayin wrote:
#include<stdio. h>
#include<string .h>
int main(void)
{
char a[3] = "ab";
char b[3] = "cd";
char c[5] = " ";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

See your problem

Other than function main not returning an int, what is the problem?

The char array a[3] has been initialized and a points to a string "ab".
The char array b[3] has been initialized and b points to a string "cd".
The char array c[5] is different. There is enough space in
the array to store the five space characters, ' ', but
not for a terminating null character. So the null character
is not included and c is simply an array of five space
characters. It is not a string. But this is not a problem in this
code.
The function strcpy copies the string pointed to by a into the
char arrary pointed to by c. c is now a string "ab".
The function strcat appends the string pointed to by b onto the
string pointed to by c. c now points to the string "abcd".
Since there is enough storage for the operations and there is
no overlapping of objects, the code is ok.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.com base.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #22
On Fri, 26 Sep 2003 13:49:34 -0500, Robert B. Clark
<ep****@3pynexf .pbz> wrote:
On Sat, 27 Sep 2003 00:37:31 +0800, "JC" <JC@JC.com> wrote:

<snip>
char a[2]="ab";


The string "ab" actually has three chars ('a', 'b' and the terminating NUL
character '\0'), but you've only allocated two in array a[]. <snip>


Right.

<snip>
char c[4]=" \0";


Ditto.

Note that c[] may hold only three characters plus the terminating NUL, so
you'll want to increase the allocated space in preparation for your string
concatenation below:

char c[5] = '\0'; /* 4 chars + terminating NUL */

You can't initialize an array to a single expression, because no
full-expression can have array type. Either = { '\0' } or = "" .

<snip>

- David.Thompson1 at worldnet.att.ne t
Nov 13 '05 #23
On Mon, 29 Sep 2003 04:13:32 GMT, Dave Thompson
<da************ *@worldnet.att. net> wrote:
On Fri, 26 Sep 2003 13:49:34 -0500, Robert B. Clark
<ep****@3pynex f.pbz> wrote:
Note that c[] may hold only three characters plus the terminating NUL, so
you'll want to increase the allocated space in preparation for your string
concatenation below:

char c[5] = '\0'; /* 4 chars + terminating NUL */

You can't initialize an array to a single expression, because no
full-expression can have array type. Either = { '\0' } or = "" .


You are of course correct.

What I had intended to write was

char c[5] = {'\0'};

Silly me for not proofreading carefully enough.

Thanks for the correction.
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
5830
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my NetBSD system, man strcpy gives the following prototype : char *strcpy(char * restrict dst, const char * restrict src); What's the point in returning a pointer we already know before the call? Thank you in advance for an explanation.
9
12805
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
81
7381
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
14
3723
by: zeroDontSpamtype | last post by:
Hi, Why do strcpy and strcat (and strupr and strlwr in some nonstandard implementations) return a char*? Surely the logical (and DMA-safe) )return type for these would have been void?? Thanks, James McLaughlin.
24
3158
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
3
16842
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
13
1941
by: steve yee | last post by:
strcpy() returns it's input parameter. i asked a similar question on c++, most people seem to not think it as a problemic design. how do you think of this? http://groups.google.com/group/comp.lang.c++/browse_thread/thread/fd0aa4307b91bad8
4
9546
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char *strcpy(char *s1, const char *s2) // library function
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9796
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6002
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.