Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String Concatenation Using Pointers No strcat()

Reply
Thread Tools

String Concatenation Using Pointers No strcat()

 
 
Default User
Guest
Posts: n/a
 
      12-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Dec 7, 3:04 pm, "Default User" <(E-Mail Removed)> wrote:
>
> > That's the stage at which you should have come to us. You should
> > know that generally speaking the standard library function WORK. So
> > if you are having a problem, it's likely a simple case of misuse.

>
> I have got only a few months of C so if there is something wrong, I
> always assume I need to expand my view and search for something else.


That's a puzzling and usually incorrect view. If you are inexperienced
with a tool, and it doesn't seem to be working, it's not likely that
you need a different tool. You probably need instruction in proper use
of the one you have.

> strcpy and strcat expect const char *, and because I am using char *


This makes little sense. The signature for strcpy() is:

char *strcpy(char *destination, const char *source);

Only the source, which you wouldn't want to change anyway, needs to be
const qualified. You don't generally WANT to alter a const-qualified
object. It's bad mojo.


> I haven't seen too many examples in which you can just pass the char *
> pointer to the function.


You see one up there.

> Most examples deal with char [].


This doesn't make sense. You can't pass an array into any function. It
sounds like you need to review the FAQ on arrays and pointers. A
typical use of strcpy() might be:

#include <string.h>


const char *src = "Here's a string!";

char *dest;

dest = malloc(strlen(src)+1;

/* error checking the result of malloc() is vital, but left out here */

strcpy(dest, src);




Brian
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2007
(E-Mail Removed) wrote:
> Keith Thompson <(E-Mail Removed)> wrote:
>
>> One problem with that is that strcat() has to scan the target
>> string each time before appending the new string. For a small
>> number of short strings like you have here, it's probably not a
>> big deal, but it's an O(N**2) algorithm for something that can
>> easily be done in O(N).

>
> strcat() is good enough for what I am doing now


I suggest strlcat and strlcpy, which will give you appropriate
warnings and defaults to avoid overrunning memory allocations.
See:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

for source, usage, documentation, reference to originators, etc.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
raidvvan@yahoo.com
Guest
Posts: n/a
 
      12-08-2007
Hello everyone,

Thanks for all your help. The code is working now.

Best regards,
Ovidiu Anghelidi
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String concatenation vs. string formatting Andrew Berg Python 13 07-10-2011 11:24 PM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Re: Using os.system() and string concatenation Wayne Witzel III Python 2 10-17-2004 06:24 PM
Using os.system() and string concatenation Wayne Witzel III Python 1 10-12-2004 07:29 PM
what's the difference between VHDL 93 CONCATENATION and VHDL 87 CONCATENATION? walala VHDL 3 09-18-2003 04:17 AM



Advertisments