Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2  Next

Simple Array Question

 
Jump to:  
 
Ray D.
PostPosted: Tue Aug 26, 2008 9:44 pm    Post subject: 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
PostPosted: Tue Aug 26, 2008 9:44 pm    Post subject: Re: Simple Array Question
       
"Ray D." <ray.delvecchio@gmail.com> wrote in message
news:112e2694-2fa9-4716-bdb6-f23a0c8d08d0@79g2000hsk.googlegroups.com...
Quote:
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
 

 
Jens Thoms Toerring
PostPosted: Tue Aug 26, 2008 9:44 pm    Post subject: Re: Simple Array Question
       
Sergio Perticone <g4ll0ws@gmail.com> wrote:
Quote:
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

Quote:
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
\__________________________ LINK
 

 
Bartc
PostPosted: Tue Aug 26, 2008 9:44 pm    Post subject: Re: Simple Array Question
       
"Sergio Perticone" <g4ll0ws@gmail.com> wrote in message
news:bdb169bb-4b74-4eb5-9a38-2a4c30de4afb@d1g2000hsg.googlegroups.com...
Quote:
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
 

 
pete
PostPosted: Tue Aug 26, 2008 9:44 pm    Post subject: Re: Simple Array Question
       
Ray D. wrote:
Quote:
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]

Are you trying to read an int from an array of char?

--
pete
 

 
ssylee
PostPosted: Tue Aug 26, 2008 9:52 pm    Post subject: Re: Simple Array Question
       
On Aug 26, 2:44 pm, "Ray D." <ray.delvecc...@gmail.com> wrote:
Quote:
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
PostPosted: Tue Aug 26, 2008 10:40 pm    Post subject: Re: Simple Array Question
       
On 26 Ago, 23:44, "Ray D." <ray.delvecc...@gmail.com> wrote:
Quote:
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
PostPosted: Tue Aug 26, 2008 10:43 pm    Post subject: Re: Simple Array Question
       
On 27 Ago, 00:40, Sergio Perticone <g4ll...@gmail.com> wrote:
Quote:
[snip]

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

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

s.
 

 
Gene
PostPosted: Tue Aug 26, 2008 11:20 pm    Post subject: Re: Simple Array Question
       
On Aug 26, 5:44 pm, "Ray D." <ray.delvecc...@gmail.com> wrote:
Quote:
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
PostPosted: Wed Aug 27, 2008 7:44 am    Post subject: Re: Simple Array Question
       
"Bartc" <bc@freeuk.com> wrote:

Quote:
"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
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

upominki grecja RegaƂy metalowe Shazza moja miƂoƛć - Big Cyc Torebki