Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72830

Re: Program to extract a substring from a string; compiling issues

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Program to extract a substring from a string; compiling issues
Date 2015-10-14 13:42 +0100
Organization A noiseless patient Spider
Message-ID <87y4f5aafc.fsf@bsb.me.uk> (permalink)
References <ddd5630d-29e4-4664-9f19-06aaf72dce6c@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:

> Here is a small program I have written (textbook task)
> that should extract a substring from a word. 
> Unfortunately, the compiler doesn't except my effort :)
>
> Please, take a look at the program, and at another option 
> of the last two lines from the main function - doesn't work
> either.
>
> // Program to extract a substring from a string
>
> #include <stdio.h>
>
> int string_length (const char string[]);
> void substring (char source[], int start, int count);

substring returns no value (that's what the "void" means) so unless it
changes the string in source or does something like input or output it
can't have have useful function.  And if you'd written

  void substring (const char source[], int start, int count);

(as you should have) I'd know for sure that it's a pointless function!

> int main (void)
> {
>     char word [] = "alphabetical";
>     char subword[81];
>     int start_letter = 3, count_number = 4;
>     
>     printf("Note that the length of the word is %i\n", string_length (word));
>     
>     subword [] = substring (word, start_letter, count_number);

You can't assign to an array in C, if that is what you are trying to do
(that would look more like subword = substring(...) so maybe that's not
what you are trying).

Pretty much the only viable option here is to pass into substring a
pointer to the (start of) the array into which the substring should be
copied.

<snip>
> void substring (char source[], int start, int count)
> {
>     int i;
>     char result [80];

Why 80?  You can't know know that 80 is enough, but if you've picked a
size for an array, make sure you check that it is enough.  Either refuse
to do anything if count >= 80, or only put a maximum of 79 characters
and a null into it.

>     if (count > string_length (source))

This is not a useful test.  See below.

>     {
>         for (i = start; i <= (start + count) && source[i] != '\0'; i++)
>         {
>             result [i] = source [i];

So if start is 17, i starts at 17 and this does result[17] =
source[17].  Is that what you want?

>         }
>         result [i + 1] = '\0';
>     }
>     else
>     {

Here we know that count <= string_length(source) but that's not really
very useful to know.  If the length is 100 and start is 99 even a count
of 10 goes wrong.

You mgith as well copy the characters you want and alway check to see if
the string has run out (i.e. check for the null).  Counting them first
and then doing two different things based on that count is just a waste
of time.

>         for (i = start; i <= (start + count); i++)
>         {
>             result [i] = source [i];
>         }
>         result [i + 1] = '\0';
>     }

Presumably you intended to "return result;" here.  Eeven if you did
this, it would not return the array because C functions can't return
arrays.  It would, instead, return a pointer to the local array
(technically called an object with automatic storage duration) and you
should never do that because the array is about to disappear.

> }

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 03:24 -0700
  Re: Program to extract a substring from a string; compiling issues Bartc <bc@freeuk.com> - 2015-10-14 11:49 +0100
    Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 05:17 -0700
      Re: Program to extract a substring from a string; compiling issues Bartc <bc@freeuk.com> - 2015-10-14 13:31 +0100
        Re: Program to extract a substring from a string; compiling issues Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-14 13:40 +0000
        Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 07:46 -0700
  Re: Program to extract a substring from a string; compiling issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 13:42 +0100
    Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 08:14 -0700
      Re: Program to extract a substring from a string; compiling issues Keith Thompson <kst-u@mib.org> - 2015-10-14 11:23 -0700
      Re: Program to extract a substring from a string; compiling issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 21:22 +0100

csiph-web