Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Simple Array Question

Reply
Thread Tools

Simple Array Question

 
 
Ray D.
Guest
Posts: n/a
 
      08-26-2008
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]
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      08-26-2008

"Ray D." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
ssylee
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 2:44*pm, "Ray D." <(E-Mail Removed)> 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.
 
Reply With Quote
 
Sergio Perticone
Guest
Posts: n/a
 
      08-26-2008
On 26 Ago, 23:44, "Ray D." <(E-Mail Removed)> 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);


 
Reply With Quote
 
Sergio Perticone
Guest
Posts: n/a
 
      08-26-2008
On 27 Ago, 00:40, Sergio Perticone <(E-Mail Removed)> wrote:
> [snip]
>
> strncpy(buf, Message+i, i+3);


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

s.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-26-2008
Sergio Perticone <(E-Mail Removed)> wrote:
> On 27 Ago, 00:40, Sergio Perticone <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      08-26-2008

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

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


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

--
Bartc

 
Reply With Quote
 
Gene
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 5:44*pm, "Ray D." <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      08-27-2008
"Bartc" <(E-Mail Removed)> wrote:

> "Sergio Perticone" <(E-Mail Removed)> wrote in message
> > On 27 Ago, 00:40, Sergio Perticone <(E-Mail Removed)> 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
 
Reply With Quote
 
Ray D.
Guest
Posts: n/a
 
      08-27-2008
On Aug 27, 5:44*am, (E-Mail Removed) (Richard Bos) wrote:
> "Bartc" <(E-Mail Removed)> wrote:
> > "Sergio Perticone" <(E-Mail Removed)> wrote in message
> > > On 27 Ago, 00:40, Sergio Perticone <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
simple, simple array question Peter Bailey Ruby 7 04-08-2008 01:54 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments