|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
407 Master Cheater Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Jan 30, 2011 9:38 pm Post subject: [C] Reversing array with pointers |
|
|
I'm pretty much stumped logic wise...
Can anyone help me?
My goal is to make arr[] = 101, 3, 4, 89, 67, 45..
This is what I have right now, I don't know where to go from here
Code: | int main()
{
int arr[] = {45,67,89,4,3,101};
int* f = arr; //first element
int* l = arr + 5; //last element
int temp = *f;
printf("%d %d", *f, *l); //test
getchar();
getchar();
return 1;
} |
|
|
Back to top |
|
|
AhMunRa Grandmaster Cheater Supreme Reputation: 27
Joined: 06 Aug 2010 Posts: 1117
|
Posted: Sun Jan 30, 2011 10:04 pm Post subject: |
|
|
Is there a pop() function in C that would remove the last element? Then you could just write that to a new array, loop through and you are done.
Why declare tmp and not use it.
_________________
<Wiccaan> Bah that was supposed to say 'not saying its dead' lol. Fixing >.> |
|
Back to top |
|
|
407 Master Cheater Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Jan 30, 2011 10:23 pm Post subject: |
|
|
AhMunRa wrote: | Is there a pop() function in C that would remove the last element? Then you could just write that to a new array, loop through and you are done.
Why declare tmp and not use it. | Oh, it was an incomplete code I'm working on, and I'm supposed to only use one array, for the declaration.
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Jan 30, 2011 11:41 pm Post subject: |
|
|
you're on the right track.
Code: | temp = *f;
*f = *l;
*l = temp; |
swapped, now loop and adjust the counters.
|
|
Back to top |
|
|
407 Master Cheater Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Jan 30, 2011 11:58 pm Post subject: |
|
|
slovach wrote: | you're on the right track.
Code: | temp = *f;
*f = *l;
*l = temp; |
swapped, now loop and adjust the counters. |
Code: | #include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[] = {45,67,89,4,3,101};
int *arrP = arr;
int* f;
int first;
int i, j;
for(i = 5, j = 5; i >= 0; i--, j--)
{
//first = *arr;
*arr = *(arr+i);
printf("%d ", *(arr+i));
}
getchar();
getchar();
return 0;
} |
Output gives me 101, 3, 4, 89, 67, 67 instead of 45. The problem is the swapping of the last digit, right? How can I work my way around it?
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jan 31, 2011 12:11 am Post subject: |
|
|
simpler yet, how about something like
Code: | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int len = sizeof(arr) / sizeof(int);
int* p1 = &arr[0]; //low
int* p2 = &arr[len - 1]; //high
int i, temp;
while(p1 < p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
} |
|
|
Back to top |
|
|
407 Master Cheater Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Mon Jan 31, 2011 12:16 am Post subject: |
|
|
slovach wrote: | simpler yet, how about something like
Code: | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int len = sizeof(arr) / sizeof(int);
int* p1 = &arr[0]; //low
int* p2 = &arr[len - 1]; //high
int i, temp;
while(p1 < p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
} |
| Yeah, that seems a LOT simpler..
But the instructions say that I could only use one pair of square brackets, which is for initializing the array.
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jan 31, 2011 12:19 am Post subject: |
|
|
Code: | int* p1 = arr; //low
int* p2 = arr + (len - 1); //high |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|