| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Noah Spitzer-Williams" |
| Date: |
23 Sep 2003 01:51:40 AM |
| Object: |
Get element number of array when using pointers? |
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
Now my problem is sometimes i don't just want to do image++, i want
to jump around in image but to know where i want to jump to, i need to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
So as you see, the location where I want to jump relies on where I
currently am.
Thanks!
- Noah
.
|
|
| User: "Michael Winter" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 06:24:00 AM |
|
|
"Noah Spitzer-Williams" wrote on 23 Sept 03:
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
Now my problem is sometimes i don't just want to do image++, i
want
to jump around in image but to know where i want to jump to, i need
to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
So as you see, the location where I want to jump relies on where
I
currently am.
Thanks!
- Noah
As long as I understand you're situation correctly, it's simple.
// your array with 'n' elements:
unsigned char image[ n ];
// pointer you use to access the elements of 'image':
unsigned char* p = image;
// current location in array:
unsigned long i = 0;
p[ i ] = some_new_value;
// move to a different location by a
// fixed amount (limited to array bounds)
i = (i+skip_size) % n;
p[ i ] = some_other_value;
// call function that requires address
// of current array element
useAddress( p );
If you really don't need the pointers, or &image[ i ] will suffice
when you need an address, you could make the above cleaner by losing
the pointer and subscripting the array directly:
// your array with 'n' elements:
unsigned char image[ n ];
// current location in array:
unsigned long i = 0;
image[ i ] = some_new_value;
// move to a different location by a
// fixed amount (limited to array bounds)
i = (i+skip_size) % n;
image[ i ] = some_other_value;
// call function that requires address
// of current array element
useAddress( &image[ i ] );
I hope I understood the situation properly so this is actually
relevant,
Mike
--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
.
|
|
|
| User: "Noah Spitzer-Williams" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 11:53:39 AM |
|
|
Hello Michael,
That all does work, however my problem was when I was looping
through each element. For example if I do:
for (int i = 0; image[i]; i++) {
// if image[i] == 0, the for loop exits when I haven't reached
the end of the image array
}
That is my major problem... How do I tell if I'm at the end of my
array or just at an element who's value is 0?
- Noah
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<k3Wbb.2947$vN5.26760311@news-text.cableinet.net>...
"Noah Spitzer-Williams" wrote on 23 Sept 03:
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
Now my problem is sometimes i don't just want to do image++, i
want
to jump around in image but to know where i want to jump to, i need
to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
So as you see, the location where I want to jump relies on where
I
currently am.
Thanks!
- Noah
As long as I understand you're situation correctly, it's simple.
// your array with 'n' elements:
unsigned char image[ n ];
// pointer you use to access the elements of 'image':
unsigned char* p = image;
// current location in array:
unsigned long i = 0;
p[ i ] = some_new_value;
// move to a different location by a
// fixed amount (limited to array bounds)
i = (i+skip_size) % n;
p[ i ] = some_other_value;
// call function that requires address
// of current array element
useAddress( p );
If you really don't need the pointers, or &image[ i ] will suffice
when you need an address, you could make the above cleaner by losing
the pointer and subscripting the array directly:
// your array with 'n' elements:
unsigned char image[ n ];
// current location in array:
unsigned long i = 0;
image[ i ] = some_new_value;
// move to a different location by a
// fixed amount (limited to array bounds)
i = (i+skip_size) % n;
image[ i ] = some_other_value;
// call function that requires address
// of current array element
useAddress( &image[ i ] );
I hope I understood the situation properly so this is actually
relevant,
Mike
.
|
|
|
| User: "Michael Winter" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 06:12:19 PM |
|
|
"Noah Spitzer-Williams" wrote on 23 Sept 03:
Hello Michael,
That all does work, however my problem was when I was looping
through each element. For example if I do:
for (int i = 0; image[i]; i++) {
// if image[i] == 0, the for loop exits when I haven't
reached
the end of the image array
}
That is my major problem... How do I tell if I'm at the end of my
array or just at an element who's value is 0?
Well, you *have* to know the size of the array, whether it is
dynamically allocated (with new[n]), explicitly sized (char image[n]),
or implicitly sized when initialised (char image[]=...). If you want
the loop to end when either the end of the array is reached or if the
current element is zero, then you'd use something like this:
for( int i = 0; 0 != image[ i ] && i < n; i++ )
{
// do whatever
}
where n above is the size of the array. If you wanted the skip
approach, you'd do something like this:
for( int i = 0, j = 0; 0 != image[ j ] && i < n; i++, j = (j + skip) %
n )
{
// do whatever, using j to index the array
}
where n above is the size of the array, i is the number of elements
covered so far, j is the current element index, and skip is the number
of elements we wish to move on each iteration (though check the syntax
of that loop!)
Any closer this time (please say, "Yes!")?
Mike
--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
[Snipped - my earlier reply]
.
|
|
|
|
| User: "Kevin Goodsell" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 12:21:14 PM |
|
|
Noah Spitzer-Williams wrote:
Hello Michael,
<snip>
Please stop top-posting.
http://www.parashift.com/c++-faq-lite/how-to-post.html
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
.
|
|
|
|
|
|
| User: "lallous" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 02:30:17 AM |
|
|
Hi,
Why not directly access the image array as:
for (int i=0;image[i];i++)
{
cout << image[i];
}
"Noah Spitzer-Williams" <noahsw@cyberdude.com> wrote in message
news:3dc5ea8a.0309222251.5215d28@posting.google.com...
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
Now my problem is sometimes i don't just want to do image++, i want
to jump around in image but to know where i want to jump to, i need to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
So as you see, the location where I want to jump relies on where I
currently am.
Thanks!
- Noah
.
|
|
|
| User: "Noah Spitzer-Williams" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 10:15:00 AM |
|
|
Because if image[i] = 0, it stops the for loop prematurely... even if
the actual element does exist
I don't know the size of image either.
The reason I was doing image++ is because it would go to the next
element in the array... it was working...
Thanks!
- Noah
"lallous" <lallous@lgwm.org> wrote in message news:<bkosq0$474d7$1@ID-161723.news.uni-berlin.de>...
Hi,
Why not directly access the image array as:
for (int i=0;image[i];i++)
{
cout << image[i];
}
"Noah Spitzer-Williams" <noahsw@cyberdude.com> wrote in message
news:3dc5ea8a.0309222251.5215d28@posting.google.com...
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
Now my problem is sometimes i don't just want to do image++, i want
to jump around in image but to know where i want to jump to, i need to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
So as you see, the location where I want to jump relies on where I
currently am.
Thanks!
- Noah
.
|
|
|
|
| User: "Kevin Goodsell" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 02:46:30 AM |
|
|
lallous wrote:
Hi,
Please stop top-posting.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
.
|
|
|
| User: "lallous" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 07:03:05 AM |
|
|
"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:qTSbb.168$RW4.0@newsread4.news.pas.earthlink.net...
lallous wrote:
Hi,
Please stop top-posting.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Is that any better?
--
Elias
.
|
|
|
| User: "Default User" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 11:32:15 AM |
|
|
lallous wrote:
"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:qTSbb.168$RW4.0@newsread4.news.pas.earthlink.net...
lallous wrote:
Hi,
Please stop top-posting.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Is that any better?
Somewhat, but please snip extraneous material, especially quoted .sig
files.
Brian Rodenborn
.
|
|
|
|
|
|
|
| User: "Kevin Goodsell" |
|
| Title: Re: Get element number of array when using pointers? |
23 Sep 2003 02:15:36 AM |
|
|
Noah Spitzer-Williams wrote:
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image[]
do {
cout << "image byte is: " << *image << endl;
while (image++);
You said image is an array of unsigned char. Arrays are non-modifiable
lvalues, so you can't apply '++' to them. This is an error.
Now my problem is sometimes i don't just want to do image++, i want
to jump around in image but to know where i want to jump to, i need to
know which element i'm on (the 1st element image[0], 3rd, 54th?)
Given a pointer 'ptr' to some element in 'image', the index of the
element is given by (ptr - image).
Is there anyway I can do this? Basically what I'm trying to do is
this:
while (image = (image + skip) % some int's)
image + skip yields a pointer, which you cannot apply '%' to. You also
can't assign to image, since it is an array.
Are you sure you don't want to use a simple index? Seems like that would
make this much easier.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
.
|
|
|
|

|
Related Articles |
|
|