Mail Archives: djgpp/2001/12/20/22:47:58
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 -