2 dimensional arrays

Discussion in 'C Programming' started by Francogrex, Feb 24, 2011.

  1. Francogrex

    Francogrex Guest

    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
    Francogrex, Feb 24, 2011
    #1
    1. Advertising

  2. Francogrex

    Mark Bluemel Guest

    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.
    Mark Bluemel, Feb 24, 2011
    #2
    1. Advertising

  3. 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.
    Ben Bacarisse, Feb 24, 2011
    #3
  4. 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.
    Ben Bacarisse, Feb 24, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    422
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,913
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    638
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    961
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page