嘿伙计们,我正在尝试完成我的代码,但我没有获取值,而是收到了错误消息。当我即将输入留置权编号 54 或 60 时。
if(*arr[rows*columns]<num) or printf("Number value %d in a two-dimensional size is:%d\n",num,*arr[num]);
这是错误消息。
Unhandled exception at 0x013137b2 in LB_12.exe: 0xC0000005: Access violation reading location 0xabababab
.
任务描述和下一张图片中的错误消息。怎么了?如果我希望程序打印值,我必须创建另一个数组并复制值吗?
这是我的代码
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void SortArray(int **arr,int rows,int columns,int num);
void freemalloc ( int **arr,int rows);
void main()
{
int **arr;
int i,j,rows,columns,num;
printf("Please enter the size of 2D array(rows ,cols)");
scanf("%d %d",&rows , &columns);
arr=(int **)malloc(rows*sizeof(int *)); // Allocate array of pointers
if(!arr) // Terms - if there is not enough memory,print error msg and exit the program.
{
printf("alloc failed\n");
return ;
}
for(i=0; i<rows; i++)
arr[i]=(int *)malloc(columns*sizeof(int)); // Allocate memory for each row
printf("Please fill the 2D array\n");
for(i=0 ; i<rows ; i++)
{
for (j=0 ; j<columns ; j++)
{
printf("row:%d columns:%d\n", i,j);
scanf("%d" , &arr[i][j]);
}
}
printf("Please enter a postive number: ");
scanf("%d",&num);
SortArray(arr,rows,columns,num);
freemalloc(arr,rows);
system("pause");
return;
}
void SortArray(int **arr,int rows,int columns,int num)
{
int i,j,temp;
for(i=0 ; i<rows ; i++ ) // Bubble sort for sorting the 2d array
{
for(j=0 ; j<i-1 ; j++ )
{
if(arr[i][j]>arr[i][j+1])
{
temp=arr[i][j];
arr[i][j]=arr[i][j+1];
arr[i][j+1]=temp;
}
}
}
if(*arr[rows*columns]<num)
{
printf("No solution,The maximum value is:%d\n",arr[rows*columns]);
}
else
{
printf("Number value %d in a two-dimensional size is:%d\n",num,*arr[num]);
}
}
void freemalloc ( int **arr,int rows)
{
int i;
for (i=0 ; i<rows ; i++) // Loop for free the array of pointers
{
free(arr[i]); // free each seprate row
}
free(arr);
}