pointers difficulties

M

Marcelo

Hello,

I have some problems with my pointers.
For exemple I have pointer as

uchar* pr;
pr = (uchar *) malloc(3 * compSize);

int i = 0;
for( i = 0 ; i< 4; i++){
uchar red = ((body >> 16) & 0xff);
*pr++ = red;
}
for( i = 4 ; i > 0; i--) {
printf("pointers: %x %x %x \n", *(pr-i));
}

As you can see my problem is that I have lost the first position of my pointer,
how can I fix this situation? Supposing that I don't want to create another pointer?


thanks

Marcelo

PS: whats the difference between
*pointer
and
pointer[value]
 
D

David White

Marcelo said:
Hello,

I have some problems with my pointers.
For exemple I have pointer as

uchar* pr;
pr = (uchar *) malloc(3 * compSize);

In particular reason for using malloc in C++? That's very C. 'new' is
better.
int i = 0;
for( i = 0 ; i< 4; i++){
uchar red = ((body >> 16) & 0xff);
*pr++ = red;
}
for( i = 4 ; i > 0; i--) {
printf("pointers: %x %x %x \n", *(pr-i));
}

As you can see my problem is that I have lost the first position of
my pointer,
how can I fix this situation?


Change *pr++ to pr or *(pr+i).
Supposing that I don't want to create
another pointer?

Then don't, but the code might be more efficient if you do. Incrementing a
pointer is likely to be at least as simple as, and possibly simpler than,
calculating an address from an index.
thanks

Marcelo

PS: whats the difference between
*pointer
and
pointer[value]

*(pointer+i) is the equivalent of pointer.

DW
 

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,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top