目标:我正在使用指针,但遇到了很多问题。上周的任务是编写一个插入排序函数来对一个参差不齐的数组进行降序排序。本周我的教授希望我更改所有索引并仅使用指针。
void insert(int **table, int row)
{
// Local Declaration
int **ptr = table;
int **walkPlus, *walk, temp;
// Statement
for(ptr = (table + 1); *ptr != NULL; ptr++)
{
temp = **ptr;
walk = *(ptr - 1);
while(*walk >= diff && temp > *walk)
{
walkPlus = ptr;
**walkPlus = *walk;
*walk--;
}
**walkPlus = temp;
printf("\n");
}
return;
}
我觉得 *walk--; 是我的问题的原因。当我使用 printf 语句检查它的值时,但我得到了一个奇怪的地址。我知道指针非常重要,我想确保我理解这个概念,所以任何帮助都将不胜感激。谢谢你。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif
#define MEM_ERROR printf("Not enough memory\n")
int** getRow(int *row);
void valiRow(int *row);
void getSize(int **table, int row);
void valiSize(int *size);
void fillTable(int **table, int row);
void bubble(int **table, int row);
void insert(int **table, int row);
void freeAlo(int **table);
int main (void)
{
// Local Declaration
int **table;
int row, i;
char answer;
int **pTable;
FILE* fpOutPut;
// Statement
fpOutPut = fopen("Output.txt", "w");
if(fpOutPut == NULL)
{
printf("Error, writing failed.\n");
exit(103);
}
do
{
table = getRow(&row);
getSize(table, row);
fillTable(table, row);
bubble(table, row);
insert(table, row);
freeAlo(table);
printf("\nDo you want to create a new ragged table? ");
printf("[y] to continue: ");
scanf(" %c", &answer);
printf("\n");
}
while(toupper(answer) == 'Y');
fclose(fpOutPut);
#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
#endif
return 0;
}// main
/* getRow */
int** getRow(int *row)
{
// Local Declaration
int **table;
// Statement
printf("Please enter the number of rows (1-10): ");
scanf("%d", &*row);
valiRow(&*row);
table =(int**)calloc(*row + 1, sizeof(int));
if(table == NULL)
MEM_ERROR, exit(100);
return table;
}
/* valiRow */
void valiRow(int *row)
{
// Statement
while(*row > 10 || *row < 1)
{
while(getchar() != '\n')
;
printf("Please enter a number between (1-10): ");
scanf("%d", &*row);
}
return;
}
/* getSize */
void getSize(int **table, int row)
{
// Local Declaration
int size;
int **ptr = table;
int **pLast = table + row;
// Statement
ptr = table;
for( ; ptr < pLast; ptr++)
{
printf("Please enter a size (1-15): ");
scanf("%d", &size);
valiSize(&size);
*ptr = (int*)calloc(size + 1, sizeof(int));
**ptr = size;
}
if(table == NULL)
MEM_ERROR, exit(101);
return;
}
/* valiSize */
void valiSize(int *size)
{
// Statement
while(*size > 15 || *size < 1)
{
while(getchar() != '\n')
;
printf("Please enter a valid size (1-15): ");
scanf("%d", &*size);
}
return;
}
/* fillTable */
void fillTable(int **table, int row)
{
// Local Declaration
int random;
int **ptr = table;
int *pCurr, *pWalk;
// Statement
srand(time(NULL));
for(pCurr = *ptr ; *ptr != NULL; ptr++)
{
for(pWalk = (pCurr + 1); *pWalk < *pCurr; pWalk++)
{
random = -99 + rand() % 199;
*pWalk = random;
}
pCurr = *(ptr + 1);
}
return;
}
/* bubble */
void bubble(int **table, int row)
{
// Local Declaration
int **ptr;
int *pWalk;
int temp, target;
// Statment
for(ptr = table; *ptr != NULL; ptr++)
{
for(target = **ptr; target > 0; target--)
{
for(pWalk = *ptr + target; pWalk != *ptr + 1; pWalk--)
{
if(*pWalk > *(pWalk - 1))
{
temp = *pWalk;
*pWalk = *(pWalk - 1);
*(pWalk - 1) = temp;
}
}
}
}
return;
}
/* insert */
void insert(int **table, int row)
{
// Local Declaration
int **ptr = table;
int temp, *walk, **walkPlus;
// Statement
for(ptr = (table + 1); *ptr != NULL; ptr++)
{
temp = **ptr;
walk = *(ptr - 1);
while(*walk >= 0 && temp > *walk)
{
walkPlus = ptr;
**walkPlus = *walk;
*walk--;
}
**walkPlus = temp;
}
return;
}
/* freeAlo */
void freeAlo(int **table)
{
// Local Declaration
int ** ptr;
// Statement
for(ptr = table; *ptr != NULL; ptr++)
{
free(*ptr);
}
free(ptr);
return;
}