Copy string of chars

Discussion in 'C Programming' started by Coolm@x, Jan 17, 2010.

  1. Coolm@x

    Coolm@x Guest

    Hi all,
    I want to write my own explode (split) function and I need copy string
    char by char. I learn pointers and my knowledge about them isn't so big.

    int func(char delimiter, char *expression, char **values) {
    (...)
    //why I can't write this:
    memcpy(values[tmplen-1],expression,1);

    //only this weird expression work:
    memcpy((*(values+i))+(tmplen-1),&expression[n],1);
    }

    int main() {
    char **val;
    val = malloc(sizeof(*val));
    func(';', "foo;bar", val);
    return EXIT_SUCCESS;
    }

    I know if I declare pointer to char variable:
    char *word;
    word = malloc(5*(sizeof(*word)));

    '[]' - works as '*' operator (If i read good tutorials :) )
    word[0] // same as *word
    ....
    word[4] // *(word+4)

    so why first way to use of 2d array of chars doesn't work with memcpy,
    strcpy (and I think other similar functions)? I would appreciate any help.
    --
    Best regards,
    - Mateusz Pa³osz
    [ e-mail: matp dot sa a-t gmail dot com ]
    [ JID: ]
    [ Pomó¿ ulepszyæ usenet: http://twovoyagers.com/improve-usenet.org/ ]
    Coolm@x, Jan 17, 2010
    #1
    1. Advertising

  2. Coolm@x

    Lew Pitcher Guest

    On January 17, 2010 16:22, in comp.lang.c, wrote:

    > Hi all,
    > I want to write my own explode (split) function and I need copy string
    > char by char.


    Looking ahead to your code, I wonder why you made a simple thing so
    complicated. A character-by-character copy is pretty simple to code, as is
    a string split function. You've really taken the long way around on this
    one.

    > I learn pointers and my knowledge about them isn't so big.


    > int func(char delimiter, char *expression, char **values) {
    > (...)
    > //why I can't write this
    > memcpy(values[tmplen-1],expression,1);


    1) What is tmplen? How is it computed
    2) /values/ is declared as a pointer_to pointer_to char. But, you want to
    use it as a two-dimensional array. Well, to find values[i+1], the compiler
    will need to know how many values[][] there are. At this point in your
    code, you have no way to tell the compiler the number of 2nd-level elements
    in your (intended) 2-dimensional array. So, the compiler won't be able to
    determine where values[i-1][...] ends and values[...] begins. That's why
    this statement fails; the maximum value of the 2nd array qualifier is
    unknown to the compiler, and you have no way of making it known.


    > //only this weird expression work:
    > memcpy((*(values+i))+(tmplen-1),&expression[n],1);


    Again, what is tmplen?

    Read the expression (specifically, the (*(values+i))+(tmplen-1) part) and
    see what it does...

    The first argument of your call to memcpy():
    values is a pointer_to pointer_to char
    values+1 is a pointer_to pointer_to char, one past values
    *(values+1) is a pointer_to char, the 2nd in a (supposed) pointer array
    tmplen is (presumably) an integer
    tmplen-1 is (presumably) an integer, one less than tmplen
    (*(values+1))+(tmplen-1) is a pointer_to char, pointing to the tmplen'th
    character of the (supposed character array) pointed to by (values+1)

    The 2nd argument of your call to memcpy()
    expression is a pointer_to char
    expression[n] is identical to *(expression+n), a char in the n+1'th
    position of the (presumed) array pointed to by expression
    &expression[n] is the address of that character, equivalent to
    (expression+n), a pointer_to char

    The 3rd argument of your call to memcpy():
    is the integer 1

    memcpy() is defined as taking three arguments:
    argument 1 is a pointer_to char that will denote the destination of the
    copy data
    argument 2 is a pointer_to char that will denote the source of the copy
    data
    argument 3 is a size_t indicating the number of characters to copy

    So, you got the arguments all set to the correct type. Should the compiler
    complain?


    > }
    >
    > int main() {
    > char **val;
    > val = malloc(sizeof(*val));
    > func(';', "foo;bar", val);


    > return EXIT_SUCCESS;
    > }
    >
    > I know if I declare pointer to char variable:
    > char *word;
    > word = malloc(5*(sizeof(*word)));
    >
    > '[]' - works as '*' operator (If i read good tutorials :) )
    > word[0] // same as *word
    > ...
    > word[4] // *(word+4)
    >
    > so why first way to use of 2d array of chars doesn't work with memcpy,
    > strcpy (and I think other similar functions)? I would appreciate any help.


    Because, the function func() doesn't know that it /is/ a 2d array, nor does
    it know the dimensions of the array. It knows only that you passed a
    pointer, and knows nothing of the object that is being pointed to.

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Jan 17, 2010
    #2
    1. Advertising

  3. Coolm@x

    Eric Sosman Guest

    On 1/17/2010 4:22 PM, Coolm@x wrote:
    > Hi all,
    > I want to write my own explode (split) function and I need copy string
    > char by char. I learn pointers and my knowledge about them isn't so big.
    >
    > int func(char delimiter, char *expression, char **values) {
    > (...)
    > //why I can't write this:
    > memcpy(values[tmplen-1],expression,1);


    Because the first two arguments are chars, not pointers.
    Let's look at the second argument first, because it's simpler.
    In expression, expression is a char*, a pointer to a char
    object. expression is the value of the i'th char after the
    one expression itself points at. You seem to want a pointer to
    that i'th character, not the character value itself. To get
    such a pointer, you could write &expression or you could
    write expression+i (the two are equivalent).

    The first argument has a similar difficulty, but with one
    more level of indirection. In values[tmplen-1], values is
    a char**, a pointer to a char* object, a pointer to a pointer
    to a char object. So values is the i'th of those char*
    pointers, and values[tmplen-1] is the tmplen-1st char in
    that i'th group. Not a pointer to the char, but the value of
    the char. You probably want one of

    &values[tmplen-1]
    or
    values+tmplen-1

    .... which are, again, equivalent.

    > //only this weird expression work:
    > memcpy((*(values+i))+(tmplen-1),&expression[n],1);


    That will work (barring the substitution of n for i), but
    it's needlessly complicated. Actually, memcpy() itself is
    needlessly complicated if all you want to do is copy one
    single character:

    values[tmplen-1] = expression[n]; /* or ... */

    > }
    >
    > int main() {
    > char **val;
    > val = malloc(sizeof(*val));
    > func(';', "foo;bar", val);
    > return EXIT_SUCCESS;
    > }
    >
    > I know if I declare pointer to char variable:
    > char *word;
    > word = malloc(5*(sizeof(*word)));
    >
    > '[]' - works as '*' operator (If i read good tutorials :) )
    > word[0] // same as *word
    > ...
    > word[4] // *(word+4)


    Yes. Formally, for any pointer p and any integer k,
    p[k] means exactly the same thing as *(p+k).

    > so why first way to use of 2d array of chars doesn't work with memcpy,
    > strcpy (and I think other similar functions)? I would appreciate any help.


    The comp.lang.c Frequently Asked Questions (FAQ) site at
    <http://www.c-faq.com/> has a good section about arrays and
    pointers. I think you might find it helpful.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 17, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Schaap

    copy a string into a 2d array of chars

    Simon Schaap, Apr 7, 2004, in forum: C Programming
    Replies:
    3
    Views:
    5,973
    Ravi Uday
    Apr 8, 2004
  2. Alex
    Replies:
    2
    Views:
    1,203
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,252
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    887
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    216
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page