# Thread: Pointer Help

1. ## Pointer Help

hi all, im new to pointers and im having problems with the program below, theres an error with pointer argument.
What i want it to basically do is to get two numbers, compare them, highest one is stored in one pointer, and the other in another pointer.

Code:
```#include <stdio.h>

void int_sort(int*,int*);

int main()
{
int val1, *pval1, val2, *pval2;

printf( "Enter two numbers: " );
scanf( "%d %d", val1, val2);

pval1 = &val1;
pval2 = &val2;

int_sort(pval1,pval2);

printf( "val1: %d\tval2: %d\n", val1, val2 );

return 0;
}

void int_sort( int *pval1, int *pval2 )
{
int store;

if ( *pval1 > *pval2 )
store = *pval1;
else
store = *pval2;
}```

2. Your assignment is wrong.

a = b;

This puts the value of b into a, not the other way around. See what you have:
Code:
```if ( *pval1 > *pval2 )
store = *pval1;
else
store = *pval2;```
Your assignment is backwards.

*pval1 = store;

This will make the value of whatever 'pval1' points at hold whatever is currently in the value 'store'. Otherwise what happens is that you put the value of 'pval1'-contents into 'store'. Since 'store' is a local variable, it is destroyed when the function ends.

Quzah.

3. Also this.
Code:
`scanf( "%d %d", val1, val2);`
Should be.
Code:
`scanf("%d %d", &val1, &val2);`

4. scanf("%d %d", &val1, &val2);
why do i need to put & on the val1 and 2 ?

i assign it just after the scanf with the pointers?

hmm

im not sure

5. You need to pass scanf an address otherwise it does not know where to put the value you entered.

6. i tried adding the ampersand, my program compiles but it doesnt work as planned..
Whats wrong with it?

7. cman, your program works with the changes people told you:
Code:
```#include <stdio.h>

void int_sort(int*,int*);

int main()
{
int val1, *pval1, val2, *pval2;

printf( "Enter two numbers: " );
scanf( "%d %d", &val1, &val2);

pval1 = &val1;
pval2 = &val2;

int_sort(pval1,pval2);

printf( "val1: %d\tval2: %d\n", val1, val2 );
return 0;
}

void int_sort( int *pval1, int *pval2 )
{
int store;

if ( *pval1 > *pval2 )
store = *pval1;
else
store = *pval2;

printf("store = %d  ",store);
}```

8. yep i know it works but its not what i want it to do.
i want to store one value in one pointer and another value in another pointer, compare the two values, and then return the larger value first then the smaller one second.

the program here just returns the higher value. sorry for the bad explanation.

9. I'm not sure I understood you, but:
Code:
```#include <stdio.h>

void int_sort(int*,int*);

int main()
{
int val1, *pval1, val2, *pval2;

printf( "Enter two numbers: " );
scanf( "%d %d", &val1, &val2);

pval1 = &val1;
pval2 = &val2;

int_sort(pval1,pval2);

printf("The smaller: %d, and the bigger: %d\n",val2,val1);
return 0;
}

void int_sort( int *pval1, int *pval2 )
{
int tmp;

//this will put the max value in val2 and the smaller in val2
if (*pval1 < *pval2) {
tmp = *pval1;
*pval1 = *pval2;
*pval2 = tmp;
}
}```
And you don't need pval1 and pval2 here, this could be like this too:
Code:
```#include <stdio.h>

void int_sort(int*,int*);

int main()
{
int val1,val2;
printf( "Enter two numbers: " );
scanf( "%d %d", &val1, &val2);

int_sort(&val1,&val2);

printf("The smaller: %d, and the bigger: %d\n",val2,val1);
return 0;
}

void int_sort( int *pval1, int *pval2 )
{
int tmp;

//this will put the max value in val2 and the smaller in val2
if (*pval1 < *pval2) {
tmp = *pval1;
*pval1 = *pval2;
*pval2 = tmp;
}
}```
But maybe you want something special, I don't know.
Ahh and you need somechanges to see if they're equal

10. Originally posted by cman
yep i know it works but its not what i want it to do.
i want to store one value in one pointer and another value in another pointer, compare the two values, and then return the larger value first then the smaller one second.

the program here just returns the higher value. sorry for the bad explanation.
You can't return more than one value, so maybe you mean you want to swap the numbers around if one is bigger than the other.

Does this help you:
Code:
```#include <stdio.h>

void changer(int *i1, int *i2)
{
int temp;
if (*i1 > *i2)
{
temp = *i1;
*i1 = *i2;
*i2 = temp;
}
}

int main(void)
{
int i, j;

i = 1;
j = 3;
changer(&i, &j);
printf ("Passing 1,3 gets %d, %d\n", i, j);

i = 3;
j = 1;
changer(&i, &j);
printf ("Passing 3,1 gets %d, %d\n", i, j);

return 0;
}```

11. thanks guys

Popular pages Recent additions