malloc for multidimensional array !!please help

S

shadab

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
(e-mail address removed)


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);
}
 
R

Richard Heathfield

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.
 
S

santosh

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.

you can run following code in C compiler and see the difference

follwoing is my code, please have a look and please reply to
(e-mail address removed)
#include <stdio.h>
#include <memory.h>

Replace above header with stdlib.h

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[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[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?
 
S

santosh

santosh said:
Replace above header with stdlib.h


Replace above with int main(void)


Do: a = malloc(lx * 2);
Check for success.

Whoops. This should be:
a = malloc(lx * sizeof *a);

<snip>
 
J

jaysome

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.

you can run following code in C compiler and see the difference

follwoing is my code, please have a look and please reply to
(e-mail address removed)
#include <stdio.h>
#include <memory.h>

Replace above header with stdlib.h

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[j] = i + j * 0.3;


Completely wrong. See FAQ 6.16.

http://c-faq.com/aryptr/dynmuldimary.html
 
S

Simon Biber

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.

#include <stdio.h>
#include <memory.h>

No such thing said:
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;
 
S

Skybuck Flying

For 2D apply these rules:

Size = height * width

Offset = width * y + x

Bye,
Skybuck.
 
P

pete

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.

you can run following code in C compiler and see the difference

follwoing is my code, please have a look and please reply to
(e-mail address removed)

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 */
 
T

Tom St Denis

Simon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top