Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C] Reversing array with pointers

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Jan 30, 2011 9:38 pm    Post subject: [C] Reversing array with pointers Reply with quote

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
View user's profile Send private message
AhMunRa
Grandmaster Cheater Supreme
Reputation: 27

Joined: 06 Aug 2010
Posts: 1117

PostPosted: Sun Jan 30, 2011 10:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Jan 30, 2011 10:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Jan 30, 2011 11:41 pm    Post subject: This post has 1 review(s) Reply with quote

you're on the right track.

Code:
temp = *f;
*f = *l;
*l = temp;


swapped, now loop and adjust the counters.
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Jan 30, 2011 11:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Jan 31, 2011 12:11 am    Post subject: Reply with quote

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
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Mon Jan 31, 2011 12:16 am    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Jan 31, 2011 12:19 am    Post subject: Reply with quote

Code:
int*   p1   = arr;      //low
int*   p2   = arr + (len - 1);   //high
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites