-
pointer again
0 2 56 647 3269
Ascending Original Descending
0 0 3269
2 2 647
56 56 56
647 647 2
3269 3269 0
Press any key to continue
Can anyone know how to print out my original?
what's wrong with my pointer?
#include<stdio.h>
#define SIZE 5
int *rev(int *);
void selectionSort(int *table);
int main(void)
{
int nums[5]={56, 0, 647, 3269, 2};
int g, *p1, *p2, *p3, nnn[5]={0};
p1=nnn;
selectionSort(nums);
p2=nums;
p3=rev(nums);
for(g=0; g<SIZE; g++)
{
nnn[g]=nums[g];
}
printf("Ascending\tOriginal\tDescending\n");
for(g=0; g<SIZE; g++)
{
printf("%d\t\t%d\t\t%d\n", *p2++, *p1++, *p3--);
}
return 0;
}
/*************** rev **************/
int *rev(int *pRev)
{
int i, numPrint;
for(i=SIZE-1, numPrint=0; i>=0; i--)
{
printf("%d ", *pRev++);
}
printf("\n\n");
return(--pRev);
}
/**********SORT *********************/
void selectionSort(int *table)
{
int col, walker, smallest, temp;
for(col=0; col<SIZE; col++)
{
smallest=col;
for(walker=col+1; walker<=SIZE; walker++)
{
if(table[walker] < table[smallest])
{
smallest = walker;
}
}
temp = table[col];
table[col] = table[smallest];
table[smallest] = temp;
}
return;
}
-
p1 = nnn[0];
I may be wrong, but the above statement should be used in the main function, not " p1 = nnn; ".
-
Every pointer assigned to a declared and defined array in your code, should point to the first element in that array.
-
p1 = nnn[0] will give you a pointer from int without a cast or differs in levels of blah blah blah warning. it depends on which compiler you use.
nnn[0] is the value stored in that element of the array, not the address. the address would be &nnn[0], which is the same as just nnn.
Crazy_C, this code:
Code:
for(g=0; g<SIZE; g++)
{
nnn[g]=nums[g];
}
should go before your calls to selectionSort and rev.
you could also use
memcpy(nnn, nums, sizeof(nums));
to copy nums to nnn if youd like.
not sure but you might have to include <string.h>.
hope this helps.
oh, yeah, this:
for(walker=col+1; walker<=SIZE; walker++)
is wrong.
should be
for(walker=col+1; walker<SIZE; walker++)
remember, int nums[5] consist of elements 0 through 4.
who knows what the value of nums[5] is.