[英]2D array - sorting | Access violation reading location error | In C
Hey guys I am trying to finish my code but instead of getting values i am getting an error msg .when i am about to enter lien number 54 or 60. 嗨,我正在尝试完成我的代码,但是当我要输入留置号码54或60时,我没有得到值,而是得到了一条错误消息msg。
if(*arr[rows*columns]<num) or printf("Number value %d in a two-dimensional size is:%d\n",num,*arr[num]);
This is error msg. 这是错误消息。
Unhandled exception at 0x013137b2 in LB_12.exe: 0xC0000005: Access violation reading location 0xabababab
. Unhandled exception at 0x013137b2 in LB_12.exe: 0xC0000005: Access violation reading location 0xabababab
。
Mission description and the error msg int he next picture. 任务说明和下一张图片的错误消息。 what is wrong?
怎么了? i have to create another array and copy the values if i want the program to print the values?
如果我希望程序打印值,我必须创建另一个数组并复制值?
This is my code 这是我的代码
#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);
}
My guess is that rows * columns
is larger than what you have allocated, meaning you try to dereference a random pointer, leading to undefined behavior and causing the crash. 我的猜测是,
rows * columns
大于分配的值,这意味着您尝试取消对随机指针的引用,从而导致未定义的行为并导致崩溃。
Also, you will never sort anything, as the outer loop condition is always false (try changing >
to <
). 另外,您永远也不会进行任何排序,因为外部循环条件始终为false(尝试将
>
更改为<
)。
My bet is : 我的赌注是:
printf("Number value %d in a two-dimensional size is:%d\n",num,*arr[num]);
You're asking for some number and put it in variable num. 您要输入一些数字并将其放入变量num中。 What makes you think that number num is first number in num-th row?
是什么让您认为数字num是第num行中的第一个数字?
There might be, and propably are more problems with this code. 此代码可能存在并且可能还会有更多问题。 EDIT:
编辑:
*arr[num]
is evaluated from right to left. *arr[num]
从右到左求值。 []
have higher precedence than *
operator. []
优先级高于*
运算符。
So it first does arr[num]
. 因此首先
does arr[num]
。 and result of that is dereferenced *(arr[num])
. 并将其结果取消引用
*(arr[num])
。 num is treated like a row, so if you don't have enough rows-you got memory violation because you are beyond array num被视为一行,因此如果您没有足够的行,则会发生内存冲突,因为您超出了数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.