Simple Array Question

Discussion in 'C Programming' started by Ray D., Aug 26, 2008.

  1. Ray D.

    Ray D. Guest

    This is probably very simple but I'm blanking and I haven't found a
    tutorial that mentions this so I figure I'll ask here - I want to
    access multiple array elements at once, as shown below. This syntax
    gives me an error, so what is the correct method? Thanks for the
    help!!

    char Message[16]
    ....
    Message[i:i+3]
    Ray D., Aug 26, 2008
    #1
    1. Advertising

  2. Ray D.

    Bartc Guest

    "Ray D." <> wrote in message
    news:...
    > This is probably very simple but I'm blanking and I haven't found a
    > tutorial that mentions this so I figure I'll ask here - I want to
    > access multiple array elements at once, as shown below. This syntax
    > gives me an error, so what is the correct method? Thanks for the
    > help!!
    >
    > char Message[16]
    > ...
    > Message[i:i+3]


    C doesn't support array slicing like this.

    You have to program your way around it. And if your intention is to obtain a
    string of 4 characters, usually you'd want a nul character at the end of
    that, a further difficulty if you want to avoid copying those 4 characters.

    --
    Bartc
    Bartc, Aug 26, 2008
    #2
    1. Advertising

  3. Ray D.

    ssylee Guest

    On Aug 26, 2:44 pm, "Ray D." <> wrote:
    > This is probably very simple but I'm blanking and I haven't found a
    > tutorial that mentions this so I figure I'll ask here - I want to
    > access multiple array elements at once, as shown below.  This syntax
    > gives me an error, so what is the correct method?  Thanks for the
    > help!!
    >
    > char Message[16]
    > ...
    > Message[i:i+3]


    I'm not so sure if you can access multiple elements of an array at
    once. I normally would access the array elements one by one inside a
    for loop (which is really fast unless you have a specific delay on the
    loop), although I'm not sure if this is what you're looking for.
    ssylee, Aug 26, 2008
    #3
  4. On 26 Ago, 23:44, "Ray D." <> wrote:
    > This is probably very simple but I'm blanking and I haven't found a
    > tutorial that mentions this so I figure I'll ask here - I want to
    > access multiple array elements at once, as shown below.  This syntax
    > gives me an error, so what is the correct method?  Thanks for the
    > help!!
    >
    > char Message[16]
    > ...
    > Message[i:i+3]


    Wrong. But you could copy the "slice" in a temporary buffer:

    char buf[ENOUGH_SPACE];

    /* ... */

    strncpy(buf, Message+i, i+3);
    Sergio Perticone, Aug 26, 2008
    #4
  5. On 27 Ago, 00:40, Sergio Perticone <> wrote:
    > [snip]
    >
    > strncpy(buf, Message+i, i+3);


    Sorry: strncpy(buf, Message+i, 3);

    s.
    Sergio Perticone, Aug 26, 2008
    #5
  6. Sergio Perticone <> wrote:
    > On 27 Ago, 00:40, Sergio Perticone <> wrote:
    > > [snip]
    > >
    > > strncpy(buf, Message+i, i+3);


    > Sorry: strncpy(buf, Message+i, 3);


    From the original post

    > Message[i:i+3]


    I would guess that you need

    strncpy( buf, Message + i, 4 );

    At least in other languages I am aware of that have such a range
    operator both the start and end element are usually included into
    the result.

    And the OP should make sure that he appends a '\0' character
    in case he wants to use the result as a string since that's
    not automatically appended by strncpy() if there's not a '\0'
    somewhere in 'Message[ i ]' to 'Message[ i + 3 ]'.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 27, 2008
    #6
  7. Ray D.

    Bartc Guest

    "Sergio Perticone" <> wrote in message
    news:...
    > On 27 Ago, 00:40, Sergio Perticone <> wrote:
    >> [snip]
    >>
    >> strncpy(buf, Message+i, i+3);

    >
    > Sorry: strncpy(buf, Message+i, 3);


    This might also need: buf[3]=0;

    --
    Bartc
    Bartc, Aug 27, 2008
    #7
  8. Ray D.

    Gene Guest

    On Aug 26, 5:44 pm, "Ray D." <> wrote:
    > This is probably very simple but I'm blanking and I haven't found a
    > tutorial that mentions this so I figure I'll ask here - I want to
    > access multiple array elements at once, as shown below.  This syntax
    > gives me an error, so what is the correct method?  Thanks for the
    > help!!
    >
    > char Message[16]
    > ...
    > Message[i:i+3]


    I ran into this kind of thing so often that I wrote a little function
    for it:

    // slice a string using Perl/Python position indexing conventions
    // use end == dst_size to slice to the end of src (or ourput buffer
    full)
    char *str_slice (char *dst, int dst_size, char *src, int beg, int end)
    {
    int len;

    if (dst_size > 0)
    {
    len = strlen (src);
    if (beg < 0)
    beg = len + beg;
    else if (beg > len)
    beg = len;
    if (end < 0)
    end = len + end;
    else if (end > len)
    end = len;
    len = end - beg;
    if (len <= 0)
    {
    dst[0] = '\0';
    }
    else
    {
    if (len >= dst_size)
    len = dst_size - 1;
    memcpy (dst, &src[beg], len);
    dst[len] = '\0';
    }
    }
    return dst;
    }
    Gene, Aug 27, 2008
    #8
  9. Ray D.

    Richard Bos Guest

    "Bartc" <> wrote:

    > "Sergio Perticone" <> wrote in message
    > > On 27 Ago, 00:40, Sergio Perticone <> wrote:


    > >> strncpy(buf, Message+i, i+3);

    > >
    > > Sorry: strncpy(buf, Message+i, 3);

    >
    > This might also need: buf[3]=0;


    Or you could start out using the right function, instead of the unwieldy
    and un-C-string-like strncpy(), and write

    char buf[SLICE_LENGTH+1];

    buf[0]='\0';
    strncat(buf, Message+i, SLICE_LENGTH);

    That way at least you know both that you'll get a properly terminated
    string, _and_ that you won't spend time filling buffers with null
    characters, no matter how large SLICE_LENGTH is and how few characters
    really are in buf.

    Alternatively, if what you really want is to copy a number of bytes, not
    a string, use memcpy().

    Richard
    Richard Bos, Aug 27, 2008
    #9
  10. Ray D.

    Ray D. Guest

    On Aug 27, 5:44 am, (Richard Bos) wrote:
    > "Bartc" <> wrote:
    > > "Sergio Perticone" <> wrote in message
    > > > On 27 Ago, 00:40, Sergio Perticone <> wrote:
    > > >> strncpy(buf, Message+i, i+3);

    >
    > > > Sorry: strncpy(buf, Message+i, 3);

    >
    > > This might also need: buf[3]=0;

    >
    > Or you could start out using the right function, instead of the unwieldy
    > and un-C-string-like strncpy(), and write
    >
    >   char buf[SLICE_LENGTH+1];
    >
    >   buf[0]='\0';
    >   strncat(buf, Message+i, SLICE_LENGTH);
    >
    > That way at least you know both that you'll get a properly terminated
    > string, _and_ that you won't spend time filling buffers with null
    > characters, no matter how large SLICE_LENGTH is and how few characters
    > really are in buf.
    >
    > Alternatively, if what you really want is to copy a number of bytes, not
    > a string, use memcpy().
    >
    > Richard


    Thanks for all the responses! I actually wrote out a quick little
    function to do what I needed. Essentially I wanted to send a string
    to an LCD in an embedded system, and the bus can accept 32-bits of
    data at a time (while the LCD is 16 characters, or 128-bits long).
    Therefore I needed to send the 16 character long string in 4 character
    increments. I noticed that the hex value of a single character was
    the correct ascii value I needed, while the value of the array of
    characters was not concatenated like I had hoped so I did end up doing
    this with a for loop and shifter:

    void WriteToLCD(char Message[LCD_LENGTH])
    {
    int i = 0;
    Xuint32 buffer = 0;

    for(i = 0; i < LCD_LENGTH; ++i)
    {
    buffer = buffer | Message;

    if ((i+1) % FSL_BUS_WIDTH_BYTES == 0)
    {
    putfslx(buffer, 0, FSL_DEFAULT);
    buffer = 0;
    }

    buffer <<= ASCII_LENGTH;
    }
    }

    LCD_LENGTH = 16
    Xuint32 = unsigned long
    FSL_BUS_WIDTH_BYTES = 4
    ASCII_LENGTH = 8
    Ray D., Aug 27, 2008
    #10
    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. Replies:
    0
    Views:
    551
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    658
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    835
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    367
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    212
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page