Re: split a string

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/14/04


Date: Mon, 14 Jun 2004 17:01:19 +0100

In article <b02d7807.0406140639.6f3457a4@posting.google.com>, Ronen Kfir
<ronenk@tauex.tau.ac.il> writes
>> size_t mystrlen(char const ** source_ptr){
>> int count = 0;
>> char const * source = *source_ptr;
>> for(count; source[count]; ++count){
>> ++(*source_ptr);
>> if(source[count] == '*') break;
>> }
>> return count;
>> }
>
>My program is written in C, as you probably can tell now...
>This is what I have done so far:
>
>#include<stdio.h>
>#include<string.h>
>#define N 3
>
>void substring(char string[N],char substrings[N]);
>
>void main()

main returns an int in both C and C++ even if your compiler is too lazy
to give you a diagnostic.

>{
>int i;
>char string[N], substrings[N];
So those are arrays of char (see below)
>
>puts("Please enter first char.");
Excuse me but this message does not agree with what follows.

>
>for(i=0;i<N;i++)
> {
> scanf ("%s", &string[i]);
But this reads a string into a single char
> puts("please enter next string.");

I think your thinking is very confused. That will not help you with your
programming. You need a clear idea of what you are doing before you can
translate it in to source code for the benefit of the compiler.
  Check fgets() from the Standard C Library as a way to get string input
(and ignore gets() which is a badly broken function)

> }
>}
>
>What I dont understand from your explenation is:
>1. What is it "const qualified string". What is the "const" in the
>function stands for?

A programming habit to protect me against stupidly changing something
which is not supposed to be changed. The parameter in this case is a
pointer to a pointer to a char. Now the way C works is to use pointers
to char to handle an array of char which is interpreted as a string.
However in this case I want to be able to tell the calling routine where
I left off in processing a string so I need a pointer to where I am in
the string. I.e. a pointer to string -> pointer to (pointer to string).

>2. Why do you use pointer for pointer here? I have just learned it, &
>I'm not quite settled with it.

Pointers are used for two major purposes in C:

1) To tell the compiler where some data is so that it can read/modify it
somewhere else.

2) To handle arrays remotely.

The reason we use char * (with or without const) to manage strings in C
is because strings are arrays of char.

A reason for a pointer parameter is that we want to be able to pass
information back to the call point in addition to the return value
(parameters used for returning information are sometimes called out
parameters.)

In this case I want to be able to return the length of the substring --
I am using the return value for that. I also want to be able to tell the
calling function where I stopped processing the array of chars it handed
me. Hence one more level of indirection.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: problem with function
    ... > If you intended copying the string, you need to use the strcpy function. ... I suggest researching hungarian notation for variable names. ... Isn't 'psz' a pointer a an array of characters containing a string? ... pointer to char. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: type of "string"
    ... Well, the above writing doesn't have anything named "String", so I can't ... The function foo is declared as taking pointer to char. ... That is why compiler gives you a warning. ...
    (comp.lang.c.moderated)
  • Re: Need to know the size of the memory block pointed to by a char*
    ... Or, use structs with both the size and the pointer, rather than ... just char* values. ... strlenwill not do the job since sometimes the string is not ... void a(int length, char *ptr) ... ...
    (comp.lang.c)
  • Re: Initialising a pointer
    ... return ptr; ... It returns a pointer to a string stored somewhere in the memory and is ... const char *fun{ ...
    (comp.lang.c)