www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/12/20/22:47:58

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "Robert B. Clark" <rclark31 AT earthlink DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: *Newbie= Why isn't this small function working correctly?
Organization: ClarkWehyr Enterprises
Message-ID: <v2b52usktbra8g4r6limg3baccdb8b7ki4@4ax.com>
References: <20011218200551 DOT 38218 DOT qmail AT web13907 DOT mail DOT yahoo DOT com>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
Lines: 60
Date: Fri, 21 Dec 2001 03:32:59 GMT
NNTP-Posting-Host: 24.183.85.252
X-Complaints-To: abuse AT home DOT net
X-Trace: news1.rdc1.mi.home.com 1008905579 24.183.85.252 (Thu, 20 Dec 2001 19:32:59 PST)
NNTP-Posting-Date: Thu, 20 Dec 2001 19:32:59 PST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Chris Amos <homie_dont_play_dat AT yahoo DOT com> wrote:

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

You need to tighten up your compiler diagnostics.  They are too relaxed.

>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;

foo.c: In function `getstr':
foo.c:26: warning: assignment of read-only location
foo.c:26: warning: assignment makes integer from pointer without a cast

>    b++;
>  }
>  *(new_string+b)='\0';

foo.c:30: warning: assignment of read-only location

In short, you cannot modify a const object.  Your compiler should have
complained about this, and you should heed that complaint.

Change your function definition to

void getstr(const char *string, char *new_string, int start_offset,
	int end_offset)

Actually, you should use size_t instead of int, or at least use unsigned
int.

Change

    *(new_string+b)=string+i;

to
	*(new_string+b) = string[i];

or even simpler

	new_string[b] = string[i];

>  return;
>}

This is a void function; strictly speaking the return statement is
unnecessary.

I'd suggest that you write your function so that it returns some useful
value--perhaps an int indicating whether the function succeeded (i.e.,
failure if end_offset < start_offset).
-- 
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://home.earthlink.net/~rclark31/ClarkWehyr.html

- Raw text -


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