943,667 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1283
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 13th, 2008
0

Reverse a pointer to a set of integers

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JpegUser is offline Offline
9 posts
since Oct 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

where you set the "ptr_size" value. Meybe it is equal to 50
Reputation Points: 20
Solved Threads: 2
Light Poster
VatooVatoo is offline Offline
39 posts
since Jan 2007
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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. }
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JpegUser is offline Offline
9 posts
since Oct 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JpegUser is offline Offline
9 posts
since Oct 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JpegUser is offline Offline
9 posts
since Oct 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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]
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Reverse a pointer to a set of integers

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JpegUser is offline Offline
9 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Vector Calculator issues (I got the code)
Next Thread in C Forum Timeline: Starting with "C"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC