By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

pointers difficulties

P: n/a
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[i] >> 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]

Dec 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Marcelo wrote:
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[i] >> 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[i] 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[i].

DW
Dec 14 '05 #2

P: n/a
David White wrote:
Marcelo wrote:
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.


That should be 'Any particular reason'.

DW
Dec 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.