Reverse a pointer to a set of integers

Reply

Join Date: Oct 2008
Posts: 9
Reputation: JpegUser is an unknown quantity at this point 
Solved Threads: 0
JpegUser JpegUser is offline Offline
Newbie Poster

Reverse a pointer to a set of integers

 
0
  #1
Nov 13th, 2008
Hi all,

Just a simple question. How do i reverse a pointer to a set of values given that i know the size of the pointer?

Eg
int *temp = malloc(ptr_size);
// values of *ptr let say is 1......100,
// if i use the algorithm below, it gets me 1..50...1 and not 100...1

ptr_size2 = ptr_size;
temp = ptr;
for (i=0;i<ptr_size;i++) {
temp[i]=ptr[ptr_size2--];
}

I also tried using temp[ptr_size] but still gives me the same result
Any idea?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 32
Reputation: VatooVatoo is an unknown quantity at this point 
Solved Threads: 2
VatooVatoo's Avatar
VatooVatoo VatooVatoo is offline Offline
Light Poster

Re: Reverse a pointer to a set of integers

 
0
  #2
Nov 13th, 2008
where you set the "ptr_size" value. Meybe it is equal to 50
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Reverse a pointer to a set of integers

 
0
  #3
Nov 13th, 2008
Problem is you are going both ways!
You are incrementing i iterator and at same time decrementing ptr_size2.
Write something like:
  1. ptr_size2 = ptr_size;
  2. temp = ptr;
  3. while (ptr_size2>=0) {
  4. temp[ptr_size-ptr_size2]=ptr[ptr_size2--];
  5. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: JpegUser is an unknown quantity at this point 
Solved Threads: 0
JpegUser JpegUser is offline Offline
Newbie Poster

Re: Reverse a pointer to a set of integers

 
0
  #4
Nov 13th, 2008
Thx for the quick response.
However after using ur codes, my compiler gave this warning:
operation on 'ptr_size2' may be undefined
and won't compile.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Reverse a pointer to a set of integers

 
0
  #5
Nov 13th, 2008
I only gave you an idea... Probably I messed up some interators, so in some point you call temp[negative]...
Check for this, it shouldn't be hard
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: JpegUser is an unknown quantity at this point 
Solved Threads: 0
JpegUser JpegUser is offline Offline
Newbie Poster

Re: Reverse a pointer to a set of integers

 
0
  #6
Nov 13th, 2008
Sorry, I still don't have any clue what is wrong. I also don't understand what is wrong with "incrementing i iterator and at same time decrementing ptr_size2"? Both are different integers, so incrementing iterator i won't affect ptr_size2 and vice versa.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Reverse a pointer to a set of integers

 
0
  #7
Nov 13th, 2008
I apologise, my bad (still early in the morning here )
I just realised that you decrease ptr_size2, not ptr_size, so it shouldn't be problem there.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: JpegUser is an unknown quantity at this point 
Solved Threads: 0
JpegUser JpegUser is offline Offline
Newbie Poster

Re: Reverse a pointer to a set of integers

 
0
  #8
Nov 13th, 2008
This is likely what happens in my loop
.
.
temp[49] = ptr[51]
temp[50] = ptr[50]
temp[51] = ptr[49]

.
.
ptr[49] is actually referring to temp[49] since they are shared pointees. So it's almost the same like

temp[51] = temp[49]
temp[52] = temp[48]
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Reverse a pointer to a set of integers

 
0
  #9
Nov 13th, 2008
Oh, yes... Your temp is actually same memory space as ptr. So:
  1. temp[0] = ptr[99]
  2. ...
  3. temp[99]=ptr[0]

But ptr[0] is temp[0], which is actually ptr[99]

You have to make a REAL copy of ptr to temp, where:
temp != ptr , but
temp[i] == ptr[i]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: JpegUser is an unknown quantity at this point 
Solved Threads: 0
JpegUser JpegUser is offline Offline
Newbie Poster

Re: Reverse a pointer to a set of integers

 
0
  #10
Nov 13th, 2008
Yes, i already tried that, as mentioned in my first post.
int temp[ptr_size];

temp[i] = ptr[i];
ptr[i]=temp[ptr_size2--];

It still gives me the same ptr result that is 1...50...1
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC