string assign to multi-dimension array w/o using pointer.

Discussion in 'C Programming' started by Makiyo, Feb 22, 2004.

  1. Makiyo

    Makiyo Guest

    how do u do something like this
    char x[3][10];
    x[0][] = "hello";

    I got an error,
    but is there a way I can do it without using pointer?
    thx ; )
     
    Makiyo, Feb 22, 2004
    #1
    1. Advertising

  2. Makiyo

    David Rubin Guest

    Makiyo wrote:
    > how do u do something like this
    > char x[3][10];
    > x[0][] = "hello";
    >
    > I got an error,
    > but is there a way I can do it without using pointer?
    > thx ; )


    strcpy(x[0], "hello");

    /david

    --
    "As a scientist, Throckmorton knew that if he were ever to break wind in
    the echo chamber, he would never hear the end of it."
     
    David Rubin, Feb 22, 2004
    #2
    1. Advertising

  3. Makiyo wrote:
    > how do u do something like this
    > char x[3][10];
    > x[0][] = "hello";
    >
    > I got an error,
    > but is there a way I can do it without using pointer?


    I guess you're trying to do this:

    char x[3][10];
    strcpy(x[0], "hello");

    You can also initialize the array like this:

    char x[3][10] = { "hello" }; /* Because initializers are missing for
    x[1] and x[2], 0 is assigned to
    every element of both arrays */


    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, Feb 22, 2004
    #3
  4. Makiyo wrote:

    > how do u do something like this
    > char x[3][10];
    > x[0][] = "hello";
    >
    > I got an error,


    That's because you can't assign to an array. x[0] is an array of 10 char,
    and an array is not a "modifiable lvalue" (I'll spare you the details), so
    you can't do what you tried to do.

    > but is there a way I can do it without using pointer?


    No. Any attempt to load x[0] involves using a pointer, although I'll agree
    that it doesn't always /look/ like it. This is because of The Rule.

    strcpy(x[0], "hello"); will work fine, for example, but it does actually use
    a pointer (because of The Rule).

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Feb 22, 2004
    #4
    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. Adam Hartshorne

    Multi-Dimension Array Question

    Adam Hartshorne, Jun 8, 2005, in forum: C++
    Replies:
    6
    Views:
    2,149
  2. Eric Laberge

    Multi-dimension array assignments

    Eric Laberge, Aug 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    464
    Dave Thompson
    Aug 27, 2005
  3. Replies:
    12
    Views:
    591
    Thad Smith
    Oct 10, 2005
  4. Replies:
    7
    Views:
    324
    Peter Nilsson
    Apr 28, 2008
  5. Luuk
    Replies:
    15
    Views:
    836
    Nobody
    Feb 11, 2010
Loading...

Share This Page