2 dimensional arrays

F

Francogrex

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
 
B

Ben Bacarisse

Francogrex said:
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
 
B

Ben Bacarisse

pete said:
Francogrex said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top