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
sanwa001@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);
}
|
|
0
|
|
|
|
Reply
|
shadab.anwar (1)
|
12/4/2006 7:49:20 AM |
|
shadab said:
> 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: 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.
|
|
0
|
|
|
|
Reply
|
rjh (10789)
|
12/4/2006 7:58:06 AM
|
|
shadab wrote:
> 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
> sanwa001@fiu.edu
<snip>
> #include <stdio.h>
> #include <memory.h>
Replace above header with stdlib.h
> main()
Replace above with int main(void)
> {
> double *a;
> int i, j, lx=5;
>
> a= (double *)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;
Completely wrong. Do:
a[i][j] = i + j * 0.3;
> 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");
> 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);
> }
|
|
0
|
|
|
|
Reply
|
santosh.k83 (3969)
|
12/4/2006 8:10:41 AM
|
|
santosh wrote:
> shadab wrote:
> > 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
> > sanwa001@fiu.edu
> <snip>
> > #include <stdio.h>
> > #include <memory.h>
>
> Replace above header with stdlib.h
>
> > main()
>
> Replace above with int main(void)
>
> > {
> > double *a;
> > int i, j, lx=5;
> >
> > a= (double *)malloc(lx*2);
>
> Do: a = malloc(lx * 2);
> Check for success.
Whoops. This should be:
a = malloc(lx * sizeof *a);
<snip>
|
|
0
|
|
|
|
Reply
|
santosh.k83 (3969)
|
12/4/2006 8:14:24 AM
|
|
On 4 Dec 2006 00:10:41 -0800, "santosh" <santosh.k83@gmail.com> wrote:
>shadab wrote:
>> 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
>> sanwa001@fiu.edu
><snip>
>> #include <stdio.h>
>> #include <memory.h>
>
>Replace above header with stdlib.h
>
>> main()
>
>Replace above with int main(void)
>
>> {
>> double *a;
>> int i, j, lx=5;
>>
>> a= (double *)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;
>
>Completely wrong. Do:
> a[i][j] = i + j * 0.3;
Completely wrong. See FAQ 6.16.
http://c-faq.com/aryptr/dynmuldimary.html
--
jay
|
|
0
|
|
|
|
Reply
|
jaysome1 (129)
|
12/4/2006 8:30:34 AM
|
|
jaysome wrote:
> On 4 Dec 2006 00:10:41 -0800, "santosh" <santosh.k83@gmail.com> wrote:
> >shadab wrote:
[...]
> Completely wrong. See FAQ 6.16.
>
> http://c-faq.com/aryptr/dynmuldimary.html
Thanks. Sorry everyone. Obviously, I shouldn't answer right after
waking up in future.
|
|
0
|
|
|
|
Reply
|
santosh.k83 (3969)
|
12/4/2006 10:47:59 AM
|
|
shadab wrote:
> 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.
>
> #include <stdio.h>
> #include <memory.h>
No such thing, should be <stdlib.h>
> main()
>
> {
>
>
> double *a;
>
> int i, j, lx=5;
>
> a= (double *)malloc(lx*2);
You want lx rows and lx columns, that's lx * lx values. Not 2 * lx. You
also need to multiply by the size of the element, since malloc expects a
number in bytes.
a = malloc(lx * lx * sizeof (double));
or
a = malloc(lx * lx * sizeof *a);
> for (i=0; i<lx; i++)
> {
> for(j=0;j<lx; j++)
> {
> *(a+i+j)= i+j*0.3;
You must multiply the row number by the width, so it will skip over that
many elements to keep each row separate.
Here the calculation is wrong again, should be
*(a + i*ix + j)
or
a[i*ix + j]
These two are equivalent.
> printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
Same here.
> }
>
> }
>
>
> 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));
Same here.
> }
>
> }
>
> free(a);
Add:
return 0;
> }
--
Simon.
|
|
0
|
|
|
|
Reply
|
news195 (400)
|
12/4/2006 11:02:34 AM
|
|
For 2D apply these rules:
Size = height * width
Offset = width * y + x
Bye,
Skybuck.
|
|
0
|
|
|
|
Reply
|
spam43 (193)
|
12/4/2006 12:17:16 PM
|
|
santosh wrote:
> jaysome wrote:
>> "santosh" <santosh.k83@gmail.com> wrote:
>> >shadab wrote:
> [...]
>> Completely wrong. See FAQ 6.16.
>>
>> http://c-faq.com/aryptr/dynmuldimary.html
>
> Thanks. Sorry everyone. Obviously, I shouldn't answer right after
> waking up in future.
The next time you do that please note stock prices, winners at the
horse races, etc. and report back here. :=)
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
12/4/2006 12:27:41 PM
|
|
shadab wrote:
>
> 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
> sanwa001@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);
> }
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double *a;
unsigned i, j;
const unsigned lx = 5;
a = malloc(lx * lx * sizeof *a);
if (a == NULL) {
puts("a == NULL");
exit(EXIT_FAILURE);
}
for (i = 0; i != lx; ++i) {
for(j = 0; j != lx; ++j) {
a[i * lx + j] = i + j * 0.3;
}
}
for (i = 0; i != lx; ++i) {
for(j = 0; j != lx; ++j) {
printf("a[%u][%u] = %f\n", i, j, a[i * lx + j]);
}
}
free(a);
return 0;
}
/* END new.c */
--
pete
|
|
0
|
|
|
|
Reply
|
pfiland (6614)
|
12/4/2006 12:50:46 PM
|
|
Simon Biber wrote:
> Here the calculation is wrong again, should be
> *(a + i*ix + j)
> or
> a[i*ix + j]
> These two are equivalent.
Better
(i*ix+j)[a]
/me ducks
hehehehe
Tom
|
|
0
|
|
|
|
Reply
|
tomstdenis (355)
|
12/4/2006 1:50:45 PM
|
|
|
10 Replies
62 Views
(page loaded in 0.128 seconds)
Similiar Articles: Help rapresenting multidimensional array ( pivot table ) - comp ...Hi all, I have a multidimensional array that rapresent category/subcategory ... How to put the mean values obtained, in an array ... Need Help Please ... How to return a 2D double array in Mex - comp.soft-sys.matlab ...Please help me. You have two major problems. ... variable memory or memory allocated with new, malloc ... Newsreader - MATLAB Central How do I return a 2d int array ... Help needed: read 3-dimensional array from a MAT-file in Fortran ...Please make sure that\n ... help needed for a grep in a multidimensional struct array - comp ... Help needed: read 3-dimensional ... Copying rows in a two dimensional array. - comp.lang.ada ...How to return a 2D double array in Mex - comp.soft-sys.matlab ... Please help me. You have two major problems ... Copying rows in a two dimensional array. - comp.lang.ada ... ... Structure array HELp - comp.soft-sys.matlabhelp needed for a grep in a multidimensional struct array - comp ... Structure array HELp ... read a field from structured array stored in netcdf file. Would you please help ... Reading/writing data to/from files into 2D array - comp.lang.vhdl ...... matrix into 4x4 blocks.I'm not sure,where to start on this.Please give ... Reading/writing data to/from files into 2D array - comp.lang.vhdl ... help reading csv file with ... Trying to implement dynamic arrays, but... - comp.lang.fortran ...Please see the code below. PROGRAM mat_5 IMPLICIT ... > I'm not a Fortran expert, but I think I can help: The ... Local Maxima of 2D array - comp.lang.idl-pvwave I'm trying ... how to read a field from structured array - comp.lang.idl-pvwave ...... have a small problem to read a field from structured array stored in netcdf file. Would you please help ... 2D - comp.lang.idl-pvwave Hello, I am now setting up a 2D array ... verilog genvar, and 2D array access - comp.arch.fpgaverilog genvar, and 2D array access Follow ... m more used with VHDL ), so, maybee I do something bad.. Could you help me please ? 2D Convolution using FFTW - comp.dsp... carray = (fftw_complex *) fftw_malloc ... re; carray[x + y * padX][1] = array[y][x].im; } } p = fftw_plan_dft_2d ... how or how not do zero paddings, please help ... malloc for multidimensional array !!please help - C / C++malloc for multidimensional array !!please help. C / C++ Forums on Bytes. Help with mallocing a 2d array please? - C BoardHelp with mallocing a 2d array please? This is a discussion on Help with mallocing a 2d array please? within the C Programming forums, part of the General Programming ... 7/24/2012 5:22:23 PM
|