PDA

View Full Version : سوال: خطای هنگام اختصاص حافظه به اشاره‌گری به آرایه



trminator
پنج شنبه 11 مهر 1392, 16:09 عصر
سلام
برنامه زیر دو ماتریس را با هم جمع می‌کند.
#include<stdio.h>
#include<stdlib.h>
#define maxcols 30
void main()
{
int nrows, ncols;
int (*a)[maxcols], (*b)[maxcols], (*c)[maxcols];
void readinput(int (*a)[maxcols], int nrows, int ncols);
void computesums(int (*a)[maxcols], int(*b)[maxcols], int(*c)[maxcols], int nrows, int ncols);
void writeinput(int(*c)[maxcols], int nrows, int ncols);
printf("how many rows?");
scanf("%d",&nrows);
printf("how many columns?");
scanf("%d",&ncols);
*a= malloc ((nrows*ncols)*sizeof(int));
*b=(int*) malloc ((nrows*ncols)*sizeof(int));
*c=(int*) malloc (nrows*ncols*sizeof(int));
printf("\n\nfirst table:\n");
readinput(a, nrows, ncols);
printf("\n\nsecond table:\n");
readinput(b, nrows, ncols);
computesums(a, b, c, nrows, ncols);
printf("\n\nsums-of the elements:\n\n");
writeinput(c, nrows, ncols);
}
void readinput(int(*a)[maxcols], int m, int n)
{
int row, col;
for(row=0;row<m;++row)
{
printf("\nenter data for no.%2d\n",row+1);
for(col=0;col<n;++col)
scanf("%d",(*(a+row)+col));
}
return;
}
void computesums(int(*a)[maxcols], int(*b)[maxcols], int(*c)[maxcols], int m, int n)
{
int row, col;
for(row=0; row<m; ++row)
for(col=0; col<n; ++col)
*(*(c+row)+col) = *(*(a+row)+col) + *(*(b+row)+col);
return;
}
void writeoutput(int(*a)[maxcols], int m, int n)
{
int row, col;
for(row = 0; row<m; ++row)
{
for(col=0; col<n; ++col)
printf("%4d",*(*(a+row)+col));
printf("\n");
}
return;
}
مشکل اینجاست که هنگام کامپایل خطا می‌دهد که در تصویر ضمیمه مشخص است. چگونه می‌توان این خطا را برطرف کرد؟

fjm11100
پنج شنبه 11 مهر 1392, 23:08 عصر
int (*a)[maxcols], (*b)[maxcols], (*c)[maxcols];
این خط باید بشه
int (*a), (*b), (*c);
و
این خطها
*a= malloc ((nrows*ncols)*sizeof(int));
*b=(int*) malloc ((nrows*ncols)*sizeof(int));
*c=(int*) malloc (nrows*ncols*sizeof(int));

باید بشن
a= (int*) malloc ((nrows*ncols)*sizeof(int));
b=(int*) malloc ((nrows*ncols)*sizeof(int));
c=(int*) malloc (nrows*ncols*sizeof(int));
ضمنا اسم تابع writeoutput است ولی writeinput را تعریف کردین و صدا زدید