# Thread: Sorting random numbers using pointing arrays.

1. ## Sorting random numbers using pointing arrays.

Code that generates 25 random numbers.
An [iarray] is set to each number.
Another array, [parray] is set to the address of the [iarray]

My only issue is when it prints out the new sorted digits, instead of printing out the actual number, it prints out the address.

I know I'm missing a few "*", just cant find where.

Any help would be appreciated.

EDIT: New code, I'm pretty sure its correct now, just feels weird.

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

#define ARRAY_SIZE 25

void print_array(int *array)
{
int x;
for(x = 0; x < ARRAY_SIZE; x++)
{
if(x != ARRAY_SIZE-1)
fprintf(stdout, "%d, ", array[x]);
else
fprintf(stdout, "%d\n", array[x]);
}
}

int main() {
int iarray[ARRAY_SIZE];

int *parray[ARRAY_SIZE];

int x, y, p, holder;

// Creating an array to point to the addresses.
for(p=0; p < ARRAY_SIZE; p++)
parray[p] = &iarray[p];

// Seed rand()
srand((unsigned int)time(NULL));

for(x = 0; x < ARRAY_SIZE; x++)
iarray[x] = (int)(rand() % 100);

fprintf(stdout, "Before Sort\n---------------\n");
print_array(iarray);

// Bubble sort method.
for(x = 0; x < ARRAY_SIZE; x++)
for(y = 0; y < ARRAY_SIZE-1; y++)
if(*parray[y] > *parray[y+1])
{
holder = *parray[y+1];
*parray[y+1] = *parray[y];
*parray[y] = holder;
}

fprintf(stdout, "\nAfter Sort\n---------------\n");
print_array(*parray);

getchar();getchar();
}``` 2. If you're using holder to hold the address of a variable that you're swapping, then to be honest with yourself/your readers/the compiler you should make holder a variable that holds addresses (i.e. a pointer). 3. Works OK for me. Although I can't explain why you need '*' when you call print_array(*parray); After 25 years of C it still confuses me. Let's just all agree that C is just WRONG. 4. Originally Posted by nonoob Works OK for me. Although I can't explain why you need '*' when you call print_array(*parray); After 25 years of C it still confuses me. Let's just all agree that C is just WRONG.
Ooh, that's a good point I missed that. print_array should take "int *array[]" and you shouldn't use a * when you pass parray into the function. 5. That's just it, having the * there is wrong. The function signature for print_array needs to be:
Code:
`void print_array(int **array)`
You can't take an array of pointers and turn it into a pointer to an array of consecutive integers.
You'll get lucky if the data was already sorted, by the fact that the first address will be the start of the array, but in all other cases it is totally wrong.

Edit: or int *array[] which was going to be my first suggestion. 6. Yeah, I figured passing a *parray was incorrect.
But when I have it there, it works, and sorts all the random numbers in order.
When i remove it, it will print out random addresses 7. You've got to change both, not one. 8. Yeah, I have

Code:
`print_array(parray)`
and

Code:
`void print_array(int **array)`

Output for that:
4454456, 4454460, 4454464... Etc 9. Aaaaaaaaaand the other part is to print the variable pointed to.

Code:
`fprintf(stdout, "%d, ", *array[x]);` Popular pages Recent additions 