Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to extract a substring from a string; compiling issues |
| Date | 2015-10-14 11:23 -0700 |
| Organization | None to speak of |
| Message-ID | <lnoag18g33.fsf@kst-u.example.com> (permalink) |
| References | <ddd5630d-29e4-4664-9f19-06aaf72dce6c@googlegroups.com> <87y4f5aafc.fsf@bsb.me.uk> <df41466b-e5e8-4182-abf3-a59ae10b58b8@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
>> > 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.
>>
> Unfortunately, I don't understand this part. I don't see
> how this functions differs from the function in the below
> program (copied from the book), which doesn't return
> anything as well.
[snip]
> void read_line (char buffer [], int size);
That read_line function doesn't directly return anything, but it does
pass information back to the caller via the object pointed to by the
`buffer` parameter. Your substring function has three parameters (and
no return value), none of which are used to pass information back to the
caller.
BTW, the parameter declaration `char bufer[]` is equivalent to
`char *buffer`, which is the way I'd write it.
You can't directly return an array value from a function. There are
several ways to do so indirectly:
- Return a pointer to (the first element of) a static array (this limits
the size of the array and clobbers the result when the function is
called again).
- Return a pointer to (the first element of) an array allocated with
malloc() (this requires the caller to deallocate the memory when it's
no longer needed).
- Require the caller to pass in a pointer to memory in which the result
will be stored, along with another parameter specifying the size (this
requires the caller to allocate memory, and to decide how much is
enough).
Other techniques are possible, but these are probably the most common.
There are language that let you return array values directly. Most of
them are implemented in C. 8-)}
[...]
> The book says that 80 is a standard width of the terminal :)
> Actually, 80 is my mistake, corrected it to 81 to account for
> a null character.
That's funny, I'm typing this into a terminal emulator whose current
width is 211 columns.
Even if you assume an 80-column terminal, it's still possible to enter
more than 80 characters on a line. As long as you cleanly handle long
lines without blowing up, you're ok. You can worry about handling
arbitrarily long input lines later.
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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