malloc for multidimensional array !!请帮忙 [英] malloc for multidimensional array !!please help
问题描述
我在检索动态二维数组中的数据时遇到了大问题。
i我正在计算并将数据保存到动态二维数组中。
但是当我检索它们时,它没有给出正确的值。
您可以在C编译器中运行以下代码并查看差异
follwoing是我的代码,请看一下,请回复
sa ****** @ fiu.edu
TIA
#include< stdio.h>
#include< memory.h>
main()
{
double * a;
int i ,j,lx = 5;
a =(double *)malloc(lx * 2);
for(i = 0; i< lx; i ++ )
{
for(j = 0; j< lx; j ++)
{
*(a + i + j)= i + j * 0.3;
printf(" a [%d] [%d] =%f \ n,i,j,*(a + i) + j));
}
}
printf(" copy data \ n \ ()= i $。
for(i = 0; i< lx; i ++)
{
for(j = 0; j< lx; j ++)
{
printf(" a [%d] [%d] =%f\ n,i,j,*(a + i +) j));
}
}
免费(a);
}
I am having big problem retrieving data assgined to dynamic 2-d array .
i am calculating and saving data in to dynamic 2-d array.
but when i retrieve them, it doesnt give correct values.
you can run following code in C compiler and see the difference
follwoing is my code, please have a look and please reply to
sa******@fiu.edu
TIA
#include <stdio.h>
#include <memory.h>
main()
{
double *a;
int i, j, lx=5;
a= (double *)malloc(lx*2);
for (i=0; i<lx; i++)
{
for(j=0;j<lx; j++)
{
*(a+i+j)= i+j*0.3;
printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
}
}
printf("copy data \n\n\n");
for (i=0; i<lx; i++)
{
for(j=0;j<lx; j++)
{
printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
}
}
free(a);
}
推荐答案
shadab说:
shadab said:
我正在检索分配给动态二维数组的数据时遇到大问题。
i我计算并将数据保存到动态二维数组中。
但是当我检索它们,它没有给出正确的值。
I am having big problem retrieving data assgined to dynamic 2-d array .
i am calculating and saving data in to dynamic 2-d array.
but when i retrieve them, it doesnt give correct values.
foo.c:6:警告:return-type默认为int'
foo.c:6:警告:功能声明不是原型
foo.c:在函数`main'':
foo.c:13:警告:隐式声明函数`malloc''
foo.c:13:警告:强制转换与函数类型不匹配
foo.c:38:警告:隐式声明函数`free''
foo.c:39:警告:控制到达无效功能的结束
首先解决这些问题,如果你还有问题再重新发布。 />
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名, - www。
foo.c:6: warning: return-type defaults to `int''
foo.c:6: warning: function declaration isn''t a prototype
foo.c: In function `main'':
foo.c:13: warning: implicit declaration of function `malloc''
foo.c:13: warning: cast does not match function type
foo.c:38: warning: implicit declaration of function `free''
foo.c:39: warning: control reaches end of non-void function
Fix these problems first, then post again if you''re still having trouble.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
shadab写道:
shadab wrote:
我在检索分配给动态二维数组的数据时遇到了大问题。
i我正在计算并将数据保存到动态二维数组中。
但是当我检索它们时,它没有给出正确的值。
您可以在C编译器中运行以下代码并查看差异
follwoing是我的代码,请看一下,请回复
sa ****** @ fiu。 edu
< snip>
<snip>
#include< stdio.h>
#include< memory.h>
#include <stdio.h>
#include <memory.h>
用stdlib.h替换上面的标题
Replace above header with stdlib.h
main()
main()
将以上替换为int main(无效)
Replace above with int main(void)
{
double * a;
int i,j,lx = 5;
a =(double *)malloc(lx * 2);
{
double *a;
int i, j, lx=5;
a= (double *)malloc(lx*2);
请:a = malloc(lx * 2);
检查是否成功。
Do: a = malloc(lx * 2);
Check for success.
for(i = 0; i< lx; i ++)
{
for(j = 0; j< lx; j ++)
{
*(a + i + j)= i + j * 0.3;
for (i=0; i<lx; i++)
{
for(j=0;j<lx; j++)
{
*(a+i+j)= i+j*0.3;
完全错误。做:
a [i] [j] = i + j * 0.3;
Completely wrong. Do:
a[i][j] = i + j * 0.3;
printf(" a [%d] [% d] =%f\\\
",I,J,*(A + I + J));
printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
再次:
printf(" a [%d] [%d] =%f \ n,i,j ,a [i] [j]);
Again:
printf("a[%d][%d] = %f\n", i, j, a[i][j]);
}
}
printf(" copy data \ n \\ n \\ n \\ n \\ n \\ n;);
for(i = 0; i< lx; i ++)
{
for (j = 0; j< lx; j ++)
{
printf(" a [%d] [%d] =%f \ n,i ,j,*(a + i + j));
}
}
}
}
printf("copy data \n\n\n");
for (i=0; i<lx; i++)
{
for(j=0;j<lx; j++)
{
printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
}
}
重复代码有什么意义?
What''s the point of repeating the code?
free(a);
}
free(a);
}
santosh写道:
santosh wrote:
shadab写道:
shadab wrote:
我在检索分配给动态二维数组的数据时遇到了大问题。
i我正在计算并将数据保存到动态二维数组中。
但是当我检索它们时,它没有给出正确的值。
你可以在C编译器中运行以下代码并查看差异
以下是我的代码,请看看并请回复
sa******@fiu.edu
< ;剪断>
<snip>
#include< stdio.h>
#include< memory.h>
#include <stdio.h>
#include <memory.h>
用stdlib.h替换上面的标题
Replace above header with stdlib.h
main()
main()
将以上替换为int main(无效)
Replace above with int main(void)
{
double * a;
int i,j,lx = 5;
a =(double *)malloc(lx * 2);
{
double *a;
int i, j, lx=5;
a= (double *)malloc(lx*2);
请:a = malloc(lx * 2);
检查是否成功。
Do: a = malloc(lx * 2);
Check for success.
哎呀。这应该是:
a = malloc(lx * sizeof * a);
< snip>
Whoops. This should be:
a = malloc(lx * sizeof *a);
<snip>
这篇关于malloc for multidimensional array !!请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!