Get element number of array when using pointers?

  • Thread starter Noah Spitzer-Williams
  • Start date
N

Noah Spitzer-Williams

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
 
K

Kevin Goodsell

Noah said:
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
 
L

lallous

Hi,

Why not directly access the image array as:
for (int i=0;image;i++)
{
cout << image;
}
 
M

Michael Winter

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
 
N

Noah Spitzer-Williams

Because if image = 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 said:
Hi,

Why not directly access the image array as:
for (int i=0;image;i++)
{
cout << image;
}

Noah Spitzer-Williams said:
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
 
N

Noah Spitzer-Williams

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++) {
// if image == 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 said:
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
 
M

Michael Winter

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++) {
// if image == 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
 

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
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top