| | |
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 |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






)