Help turning ints into pointers

I'm trying to figure out how to turn ints into pointers but every-time I try, the program errors out. So I have to revert back to using regular int i.Keep in mind it compiles fine now. Anyway, whats the best way to go about turning "int i" or any "int" for that matter in all of my functions to pointers and then making the calculations work. The int get.cpp function below is the only one I think that works successfully but it sill confuses me when I try to imitate it on other functions. The functions are all in separate files so that's why it looks split like this.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

const int N = 100;

int get(int * a, int max);
void put(int * a, int len);
bool isinorder(int * a, int len);
void selection(int * a, int len);
int findmax(int * a, int len);
void swap(int *, int *);

(next function)

1
2
3
4
5
6
7
8
9
10
11
12
13
// returns to the highest element position in a[0] - a[len]

int findmax(int * a, int len)
{
int current = 0;
int i = 1;
while (i <= len)
{
    if (*(a + i) > *(a + current)) current = i;
    i++;
}
return current;
}

(next function)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Get keyboard up to max ints, return the number typed in.

int get(int * a, int max)
{
cout << "Type some ints: " << endl;

//Below, points to first array element

int *p = a;
while (cin >> *a)
{
    a++;
    if (a - p == max) break;
}
return a - p;
}


(next function)

1
2
3
4
5
6
7
8
9
10
11
bool isinorder(int * a, int len)
{
int i = 0;
while (i < len - 1)
{
    if (*a > *(a + i)) return false;
    a++;
    i++;
}
return true;
}


(next function)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "ar.h"

int main()
{
int * a;
a = new int[N]; // Get N new intergers during runtime.

int len = get(a,N);

cout << "Elements in original order: " << endl;
put(a, len);
cout << endl;

if (isinorder(a,len)) cout << "in order!" << endl;
else
{
    selection(a,len);
    cout << "Elements in sorted order: " << endl;
    put(a, len);
    cout << endl;
}
return 0;
}

(next function)

1
2
3
4
5
6
7
8
9
10
11
12
#include "ar.h"

void put(int * a, int len)
{
int i = 0;
cout << endl;
while (i < len)
{
    cout << *(a + i) << endl;
    i++;
}
}


(next function)

1
2
3
4
5
6
7
8
9
10
11
#include "ar.h"

void selection(int * a, int len)
{
while (len--)
{
    // j points to biggest elements in array a
    int j = findmax(a, len);
    swap( a + j, a + len); //Same as swap(&a[j], &a[len]);
}
}

(last function)

1
2
3
4
5
6
void swap(int * x, int * y)
{
int temp = *x;
*x = *y;
*y = temp;
}


Below is the output I get now (which is correct). I want to get the same exact output but I need to change as many "ints" as I can into pointers. I am using a file full of numbers to compile. Thats why the numbers are random.

!g++ *.cpp && a.out < /tmp/input
Type some ints:
Elements in original order:

755
117
-249
178
180
306
-245
3048134
5
195
12372
1212
-291

Elements in sorted order:

-291
-249
-245
5
117
178
180
195
306
755
1212
12372
3048134
Last edited on
Do you mean you have an int, and you want a pointer to that int?

1
2
int x; // an int
int* y = &x; // a pointer, y, that points to the int x 
Maybe he's trying to cast an int to a pointer? :D
Maybe he's trying to cast an int to a pointer? :D


I know I love doing that, but it's not something I recommend :P
What I want to do is whats in the "get" function below. The function below used to have "int i = 0" and the calculations would be different in relation to i. But I changed the ints into pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int get(int * a, int max)
{
cout << "Type some ints: " << endl;

//Below, points to first array element

int *p = a;
while (cin >> *a)
{
a++;
if (a - p == max) break;
}
return a - p;
}


Now how could I use the same style of using pointers for int i for the example below for instance. Notice that in the "if" line I already have pointers and it works fine. All of this is part of a project I need to do. I wouldnt typically try this either but I must.

1
2
3
4
5
6
7
8
9
10
11
bool isinorder(int * a, int len)
{
int i = 0;
while (i < len - 1)
{
if (*a > *(a + i)) return false;
a++;
i++;
}
return true;
}


I tried "int *p = a. and fooled around with the rest but it wont work.
Last edited on
Please use code embedding, that way we can at least refer to what line you are off.
Im trying to turn all of the "int i = 0" or int i = 1" into pointers.
I know its something to the effect of "int * p = a" but changing the rest of the function below the decleration is what gives me problems.
Last edited on
I don't understand.
1
2
3
4
5
6
int get(int * a, int max){
   size_t K;
   for(K=0; K<max and cin>>a[K]; ++K)
      ;
   return K;
}


Also
1
2
const int N = 100;
a = new int[N]; // Get N new intergers during runtime 

You know the size at compile time. It's not too big.
¿why are you using dynamic allocation?

I think I get it
You don't want to use a counter, but just pointers. ¿why?
1
2
3
4
5
6
7
8
bool isinorder(int * a, int len){
   int *end = a+len;
   while (a < end-1){
      if (*a > *(a + 1)) return false;
      a++;
   }
   return true;
}
Last edited on
ne555, I think you’re on the right track with what you did in bool isinorder. Once I can get my ssh secure shell client working again I will work with that and see how it comes out.

I don’t personally want to make these into all pointers. It’s part of a bigger project we are doing in class.

As for compiling, we pull a file that has a list of numbers. We just have the size set to 100 even though there is probably only 20 numbers in the file.
You program might crash if you are trying to do something like

1
2
3
4
5

int* p = (int*)0xFFFFAABC;

*p = [some random integer here];


Last edited on
Lestor77 wrote:
Im trying to turn all of the "int i = 0" or int i = 1" into pointers.

its simple!
1
2
int* a;
*a=0;//or 1 
1
2
int* a;
*a=0;//or 1 


This is bad practice. Dereferencing an uninitialized pointer yields undefined results.
I think I get it
You don't want to use a counter, but just pointers. ¿why?


1
2
3
4
5
6
7
8
bool isinorder(int * a, int len){
   int *end = a+len;
   while (a < end-1){
      if (*a > *(a + 1)) return false;
      a++;
   }
   return true;
} 


ne555. I think what you did here hit it spot on. This is what I'm looking for. I can easily change the int i into pointers. Its just changing the calculations below to work with it confuses me. Thanks.
Topic archived. No new replies allowed.