473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 19 '05 #1
10 6528
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.

Jul 19 '05 #2
Hi,

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

"Noah Spitzer-Williams" <no****@cyberdude.com> wrote in message
news:3d*************************@posting.google.co m...
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

Jul 19 '05 #3
lallous wrote:
Hi,


Please stop top-posting.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
"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)
Jul 19 '05 #5
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:qT*************@newsread4.news.pas.earthlink. net...
lallous wrote:
Hi,


Please stop top-posting.

http://www.parashift.com/c++-faq-lit...t.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
Jul 19 '05 #6
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" <la*****@lgwm.org> wrote in message news:<bk************@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" <no****@cyberdude.com> wrote in message
news:3d*************************@posting.google.co m...
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

Jul 19 '05 #7
lallous wrote:

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:qT*************@newsread4.news.pas.earthlink. net...
lallous wrote:
Hi,


Please stop top-posting.

http://www.parashift.com/c++-faq-lit...t.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
Jul 19 '05 #8
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:<k3*********************@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

Jul 19 '05 #9
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-lit...t.html#faq-5.4

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10
"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]
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
2
by: NickPomp | last post by:
Hi, I have to write a slide puzzle program for class. I have the program finished and working except that I can not get the blank space to print out. I wrote code that would find the number I used...
4
by: sahil | last post by:
Hello frends i am learning c language, I want to make a program which count occurence of each element in an array .I write following code for it but ity is not giving me desired result.pls help me....
7
by: Szabolcs Borsanyi | last post by:
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a) { int...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
10
by: arcadio | last post by:
Hi everyone, I'm currently struggling to compile a large piece of legacy code. GCC 3.3 compiles it without complaining, but GCC 4.2.3 (the default in Debian) refuses it and signals "several...
2
by: Gestorm | last post by:
Suppose we have an array a, the idea is: build another array, int next, for each 0<i<N, next = next position of a in the sorted array, if a is the max, then next is undefined. For example, let the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.