Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Simple Array Question (http://www.velocityreviews.com/forums/t632486-simple-array-question.html)

Ray D. 08-26-2008 09:44 PM

Simple Array Question
 
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]

Bartc 08-26-2008 09:50 PM

Re: Simple Array Question
 

"Ray D." <ray.delvecchio@gmail.com> wrote in message
news:112e2694-2fa9-4716-bdb6-f23a0c8d08d0@79g2000hsk.googlegroups.com...
> 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


ssylee 08-26-2008 09:52 PM

Re: Simple Array Question
 
On Aug 26, 2:44*pm, "Ray D." <ray.delvecc...@gmail.com> 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.

Sergio Perticone 08-26-2008 10:40 PM

Re: Simple Array Question
 
On 26 Ago, 23:44, "Ray D." <ray.delvecc...@gmail.com> 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 08-26-2008 10:43 PM

Re: Simple Array Question
 
On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> wrote:
> [snip]
>
> strncpy(buf, Message+i, i+3);


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

s.

Jens Thoms Toerring 08-26-2008 11:00 PM

Re: Simple Array Question
 
Sergio Perticone <g4ll0ws@gmail.com> wrote:
> On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> 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 ___ jt@toerring.de
\__________________________ http://toerring.de

Bartc 08-26-2008 11:02 PM

Re: Simple Array Question
 

"Sergio Perticone" <g4ll0ws@gmail.com> wrote in message
news:bdb169bb-4b74-4eb5-9a38-2a4c30de4afb@d1g2000hsg.googlegroups.com...
> On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> wrote:
>> [snip]
>>
>> strncpy(buf, Message+i, i+3);

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


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

--
Bartc


Gene 08-26-2008 11:20 PM

Re: Simple Array Question
 
On Aug 26, 5:44*pm, "Ray D." <ray.delvecc...@gmail.com> 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;
}

Richard Bos 08-27-2008 09:44 AM

Re: Simple Array Question
 
"Bartc" <bc@freeuk.com> wrote:

> "Sergio Perticone" <g4ll0ws@gmail.com> wrote in message
> > On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> 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

Ray D. 08-27-2008 08:49 PM

Re: Simple Array Question
 
On Aug 27, 5:44*am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> "Bartc" <b...@freeuk.com> wrote:
> > "Sergio Perticone" <g4ll...@gmail.com> wrote in message
> > > On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> 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[i];

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


All times are GMT. The time now is 02:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.