| | |
Reverse a pointer to a set of integers
![]() |
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
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?
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?
Problem is you are going both ways!
You are incrementing i iterator and at same time decrementing ptr_size2.
Write something like:
You are incrementing i iterator and at same time decrementing ptr_size2.
Write something like:
C Syntax (Toggle Plain Text)
ptr_size2 = ptr_size; temp = ptr; while (ptr_size2>=0) { temp[ptr_size-ptr_size2]=ptr[ptr_size2--]; }
Oh, yes... Your temp is actually same memory space as ptr. So:
But ptr[0] is temp[0], which is actually ptr[99]
You have to make a REAL copy of ptr to temp, where:
C Syntax (Toggle Plain Text)
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] ![]() |
Similar Threads
- Pointers (archived tutorial) (C++)
- What am I doing wrong. (C++)
- Linked Lists Help C++ (C++)
- data sorts in reverse order!! help!! (C)
- Pointers (C++)
Other Threads in the C Forum
- Previous Thread: Vector Calculator issues (I got the code)
- Next Thread: Starting with "C"
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






)