473,800 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc for multidimensiona l array !!please help

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.ed u
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);
}

Dec 4 '06 #1
10 1798
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.
Dec 4 '06 #2
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
sa******@fiu.ed u
<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);
}
Dec 4 '06 #3

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
sa******@fiu.ed u
<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>

Dec 4 '06 #4
On 4 Dec 2006 00:10:41 -0800, "santosh" <sa*********@gm ail.comwrote:
>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
sa******@fiu.ed u
<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
Dec 4 '06 #5
jaysome wrote:
On 4 Dec 2006 00:10:41 -0800, "santosh" <sa*********@gm ail.comwrote:
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.

Dec 4 '06 #6
For 2D apply these rules:

Size = height * width

Offset = width * y + x

Bye,
Skybuck.
Dec 4 '06 #7
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.
Dec 4 '06 #8
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
sa******@fiu.ed u

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_FAILU RE);
}
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
Dec 4 '06 #9
santosh wrote:
jaysome wrote:
>"santosh" <sa*********@gm ail.comwrote:
>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>
Dec 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1547
by: Bob Bedford | last post by:
Still having problem with array of arrays. Here is the code: echo("Name: ".$res."<br>"); //$res is an array with many MODELNAME while(list($k,$v) = each($res)){ echo("Name: ".$res."<br>"); While the first line (the echo) works fine, the echo in the "while" doesn't work.
2
1350
by: almurph | last post by:
Folks, I have an array of chars nad would like to remove any repeating terms. Any ideas as how to do this? Thanks, Al.
1
1245
by: Gena | last post by:
Hi , I'm a newbe to programming and have a small question: I made a small program with a class. in the class's header file I have : double *ptr_output; Void main() { double result;
9
987
by: Dr. Pastor | last post by:
I need a row of 127 bytes that I will use as a circular buffer. Into the bytes (at unspecified times) a mark (0<mark<128) will be written, one after the other. After some time the "buffer" will contain the last 127 marks. (A pointer will point to the next byte to write to.) What would be the Pythonic way to do the above? Thanks for any guidance.
14
2898
by: Michel Rouzic | last post by:
Hi, I've recently met issues with my program which can only be explained by heap corruption, so I've tried debugging my program with Valgrind, and here's what I get with the following multidimensional array allocation code : typedef struct { int32_t speed;
2
1426
by: carlos123 | last post by:
Ok i have a few different arrays for names, last names, address, and city , and i have a textfield for each one. Ultimatly i want my project to be able to enter the names in and hit "save" and it enteres it in to a database. Then i have a submit button, and i want it to write the database to a text file. here is what i have so far. it can be opened in bluej, but here is the code. thanks in advance for helping me out! i really need help here,...
1
1378
by: perhapscwk | last post by:
I just place some code here... I want to make it to add each char into the array qtqt,...when it reach ",".. it will start to place char into another qtqt.. please help.thanks var qtqt=new Array() var y=0
2
1149
by: almurph | last post by:
Folks, Hope you can help me here. I am using MS Visual Studio 2008 and the .NET framework v3.5. I have built WCF Service Application with a method that returns a char array. I have imported this web service into a windows application but I see that it now returns an int array. I'm very confused! I have checked the return type in the web service
4
2393
by: SquidgeyBall | last post by:
I have created a multidimensional array as follows: string myarray = {{"TestArray1Part1", "TestArray1Part2"},{"TestArray2Part1", "TestArray2Part2"}}; Basically what I want to do is show a message box which will display the first two parts of the array (TestArray1Part1 & TestArray1Part2), then loop to show the next parts. I am currently using a foreach statement to loop through the array but this just shows each item one by one. Do...
0
9694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10509
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10256
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6824
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2953
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.