473,657 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6549
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****@cyberdu de.com> wrote in message
news:3d******** *************** **@posting.goog le.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

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_valu e;
// 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_valu e;
// 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.u k (remove [no-spam] to reply)
Jul 19 '05 #5
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:qT******** *****@newsread4 .news.pas.earth link.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.o rg> 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****@cyberdu de.com> wrote in message
news:3d******** *************** **@posting.goog le.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

Jul 19 '05 #7
lallous wrote:

"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:qT******** *****@newsread4 .news.pas.earth link.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.u k> 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_valu e;
// 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_valu e;
// 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

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

Similar topics

2
2041
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 char array strarr holds garbage characters. For some reason, every time the strcpy function is called to copy the string in temp to the array, it fills the 0th element in the strarr array with garbage characters. Can someone try to compile this...
1
8697
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 Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
5
3678
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 elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the
2
1937
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 for the blank space. I have the puzzle print out using two for loops, then before the line that prints it I used an IF statement to find the number. I have tried using a break statement, but it does not finish the rest of the for loop for the...
4
16274
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. #include<stdio.h> #include<conio.h> void main() { int A,f,a,b,c; printf("enter 10 elements"); for(a=0;a<=9;a++)
7
1689
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 *b=(void*)a; int *c=a; /*...*/ } Now the questions come:
17
2313
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 same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
10
7621
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 array has incomplete element type" errors. I know that since 4.0 or so, GCC is less forgiving and does not accept any arrays of incomplete type (see http://gcc.gnu.org/ml/gcc/2005-02/msg00053.html). However, I cannot see where the array types are...
2
2851
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 unsorted array is a = {5, 4, 2, 1, 3}; then next would be {undefined, 0, 4, 2, 1} after sorted.
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.