delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/12/18/15:33:59

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: *Newbie= Why isn't this small function working correctly?
Date: 18 Dec 2001 20:25:54 GMT
Organization: Cornell University
Lines: 55
Sender: asu1 AT cornell DOT invalid (on 128.253.251.163)
Message-ID: <Xns917B9BF02CCE5ASINANUNUR@132.236.56.8>
References: <20011218200551 DOT 38218 DOT qmail AT web13907 DOT mail DOT yahoo DOT com>
NNTP-Posting-Host: 128.253.251.163
X-Trace: news01.cit.cornell.edu 1008707154 22422 128.253.251.163 (18 Dec 2001 20:25:54 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 18 Dec 2001 20:25:54 GMT
User-Agent: Xnews/4.06.22
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

First off, this is not a C group so this post is off-topic here. You should 
try comp.lang.c.moderated or comp.lang.c. I also suggest you take a look at 
the FAQ list for that group (http://www.eskimo.com/~scs/C-faq/top.html).

Chris Amos <homie_dont_play_dat AT yahoo DOT com> wrote in
news:20011218200551 DOT 38218 DOT qmail AT web13907 DOT mail DOT yahoo DOT com: 

> It has to be a logic error of some kind because the code runs fine
> aside from a few warning messages...

ahem ... please always compile your code with -Wall, and pay attention to 
those warnings ... they mean something.

C:\var>cat s.c
void getstr(const char *string, const char *new_string, int
start_offset, int end_offset)
{
int i=0;
int b=0;
  for (i=start_offset; i<=end_offset; i++) {
   *(new_string+b)=string+i;
   b++;
 }
 *(new_string+b)='\0';
 return;
}

C:\var>gcc -Wall -c s.c
s.c: In function `getstr':
s.c:7: warning: assignment of read-only location
s.c:7: warning: assignment makes integer from pointer without a cast
s.c:10: warning: assignment of read-only location

you should think about what these warnings mean, and eliminate the reasons 
for them.

now, obviously you don't really need this do you?

assuming 

* new_string points to a large enough storage area
* start_offset is the index of the character you want to start copying from
* end_offset is the index of the character up to which will copy

there is nothing wrong with

strncpy(new_string, string + start_offset, end_offset - start_offset)

HTH.

Sinan.
-- 
--------------------------------
A. Sinan Unur
http://www.unur.com/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019