Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 2 dimensional arrays

Reply
Thread Tools

2 dimensional arrays

 
 
Francogrex
Guest
Posts: n/a
 
      02-24-2011
I'm confused about 2 dimensional arrays and there memory storage and
how to handle them using pointers; for example below something is
wrong but can't spot why?

#include <stdio.h>
#include <stdlib.h>

double **test ()
{
double **arr =malloc(100);
arr[0][0] = 5.6;
arr[0][1]=17.4;
return arr;
}

int main ()
{
double **myarray;
myarray=test();
printf("%f -- %f", myarray[0][0],myarray[1][1]);
}

// result: 5.600000 -- 17.400000 but expected to give error of
myarray[1][1] not 17.400000
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      02-24-2011
On 02/24/2011 11:35 AM, Francogrex wrote:
> I'm confused about 2 dimensional arrays and there memory storage and
> how to handle them using pointers;


Have you read section 6 of the CLC FAQ? <http://c-faq.com>
You probably should - especially question 6.16.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-24-2011
Francogrex <> writes:

> I'm confused about 2 dimensional arrays and there memory storage and
> how to handle them using pointers; for example below something is
> wrong but can't spot why?
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double **test ()
> {
> double **arr =malloc(100);
> arr[0][0] = 5.6;
> arr[0][1]=17.4;
> return arr;


Arrays are not pointers and pointers are not arrays. Your malloc line
allocates 100 bytes (that's odd in itself, but you're the boss) and
points to it with a pointer that will interpret those 100 bytes as a
sequence of pointers to doubles. Until you set those pointers to point
to something valid, you can't use arr[0][0] at all.

Your best bet is to start with the FAQ. It has a summary of how 2D
arrays can be allocated and used in C:
http://c-faq.com/aryptr/dynmuldimary.html

The FAQ is a little old, and if you have access to a compiler that
supports what are called variably modified arrays, you can often write
much neater 2D manipulation functions.

> }
>
> int main ()
> {
> double **myarray;
> myarray=test();
> printf("%f -- %f", myarray[0][0],myarray[1][1]);
> }
>
> // result: 5.600000 -- 17.400000 but expected to give error of
> myarray[1][1] not 17.400000


--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-24-2011
pete <> writes:

> Francogrex wrote:
>> I'm confused about 2 dimensional arrays and there memory storage and
>> how to handle them using pointers; for example below something is
>> wrong but can't spot why?
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> double **test ()
>> {
>> double **arr =malloc(100);
>> arr[0][0] = 5.6;
>> arr[0][1]=17.4;
>> return arr;
>> }

>
> &(arr[0][1]) - &(arr[0][0]) is equal to one;


No, both &arr[0][1] and &arr[0][0] are undefined. The & and the *
implied by the []s cancel and your get arr[0]+1 - arr[0] and arr[0] is
not been given a value at any point in the program.

I suspect you intended to describe what should be the case in a proper
2D array but the declaration of 'arr' makes it a pointer to the start of
a plain 1D array.

> but there is no way to tell from the above code
> how much &(arr[1][0]) - &(arr[0][0]) is supposed to be,
> which is something that the compiler needs to know
> in order to implement arr as a two dimensional array.


--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57