| | |
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 api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






)