| | |
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 * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






)