Simple Array Question

R

Ray D.

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]
 
B

Bartc

Ray D. said:
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.
 
S

ssylee

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.
 
S

Sergio Perticone

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);
 
J

Jens Thoms Toerring

Sergio Perticone said:
[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
 
G

Gene

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;
}
 
R

Richard Bos

Bartc said:
Sergio Perticone said:
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
 
R

Ray D.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top