954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reverse a pointer to a set of integers

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

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

where you set the "ptr_size" value. Meybe it is equal to 50 ;)

VatooVatoo
Light Poster
44 posts since Jan 2007
Reputation Points: 20
Solved Threads: 2
 

Problem is you are going both ways!
You are incrementing i iterator and at same time decrementing ptr_size2.
Write something like:

ptr_size2 = ptr_size;
temp = ptr;
while (ptr_size2>=0) {
temp[ptr_size-ptr_size2]=ptr[ptr_size2--];
}
Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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.

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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.

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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]

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Oh, yes... Your temp is actually same memory space as ptr. So:

temp[0] = ptr[99]
...
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]

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

You have to do it for EACH i.
So put it in for loop

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

yes sure that's what i did and got the result 1...50...1. It's weird and i'm sure i have checked my loops a couple of times. Perhaps any other alternatives?

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Please post entire code, because your code should work then.

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

here:

int temp[ptr_size];
int ptr_size2 = ptr_size;

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

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


The second for loop is to get back a reversed ptr.

JpegUser
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You