Assigning values to a dynamically allocated array

Discussion in 'C Programming' started by Ray D., Oct 2, 2007.

  1. Ray D.

    Ray D. Guest

    I want to set matrix A with the values below, but it produced a syntax
    error when I try to compile the commented code. I was told to use a
    for loop to do this (as shown below), but that really isn't possible
    considering I need these specific values. Is there a way to do this
    in one statement instead of multiple statements of A[j] = ...,
    where I manually specify the position? Why can you initialize like
    this with a static array, but can't use the same syntax with a dynamic
    array after the memory is allocated?

    #define MATSIZE 8
    ....
    int **A;
    ....
    A = malloc(MATSIZE*sizeof(int *));
    for (i = 0; i < MATSIZE; i++)
    A = malloc(MATSIZE*sizeof(int));

    for ( i=0; i < MATSIZE; i++ )
    {
    for ( j=0; j < MATSIZE; j++ )
    A[j] = ...;
    }

    /* A = {{0, 1, 0, 0, 0, 3, 0, 0},
    {0, 0, 0, 4, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1},
    {0, 0, 0, 1, 0, 7, 0, 0},
    {6, 0, 8, 0, 0, 0, 2, 0},
    {0, 0, 0, 0, 0, 3, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 5},
    {0, 2, 0, 0, 0, 0, 1, 0}}; */
    Ray D., Oct 2, 2007
    #1
    1. Advertising

  2. Ray D.

    Jack Klein Guest

    On Tue, 02 Oct 2007 18:53:00 -0000, "Ray D."
    <> wrote in comp.lang.c:

    > I want to set matrix A with the values below, but it produced a syntax
    > error when I try to compile the commented code. I was told to use a
    > for loop to do this (as shown below), but that really isn't possible
    > considering I need these specific values. Is there a way to do this
    > in one statement instead of multiple statements of A[j] = ...,
    > where I manually specify the position? Why can you initialize like
    > this with a static array, but can't use the same syntax with a dynamic
    > array after the memory is allocated?
    >
    > #define MATSIZE 8
    > ...
    > int **A;
    > ...
    > A = malloc(MATSIZE*sizeof(int *));
    > for (i = 0; i < MATSIZE; i++)
    > A = malloc(MATSIZE*sizeof(int));
    >
    > for ( i=0; i < MATSIZE; i++ )
    > {
    > for ( j=0; j < MATSIZE; j++ )
    > A[j] = ...;
    > }
    >
    > /* A = {{0, 1, 0, 0, 0, 3, 0, 0},
    > {0, 0, 0, 4, 0, 0, 0, 0},
    > {0, 0, 0, 0, 0, 0, 0, 1},
    > {0, 0, 0, 1, 0, 7, 0, 0},
    > {6, 0, 8, 0, 0, 0, 2, 0},
    > {0, 0, 0, 0, 0, 3, 0, 0},
    > {0, 0, 0, 0, 0, 0, 0, 5},
    > {0, 2, 0, 0, 0, 0, 1, 0}}; */


    Arrays are modifiable lvalues in C. You can't assign to them. You
    can only assign to the individual members, one-by-one. Perhaps you
    could do that in a loop.

    I would suggest that you read Section 6, "Arrays and Pointers", in the
    FAQ for this group, link in my signature. Then I'd suggest you look
    over the rest of the FAQ.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 3, 2007
    #2
    1. Advertising

  3. Jack Klein <> wrote:
    > Arrays are modifiable lvalues in C. ...


    ITYM they're *not* modifable lvalues in C. [cf 6.3.2.1p1]

    --
    Peter
    Peter Nilsson, Oct 3, 2007
    #3
    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. Jeff Williams

    Dynamically allocated array question

    Jeff Williams, Aug 18, 2003, in forum: C++
    Replies:
    6
    Views:
    487
    Alf P. Steinbach
    Aug 19, 2003
  2. Replies:
    5
    Views:
    609
    Matt Wharton
    Dec 9, 2004
  3. Replies:
    6
    Views:
    4,050
    Dave Thompson
    Feb 7, 2005
  4. Replies:
    2
    Views:
    296
    Rolf Magnus
    Jun 14, 2005
  5. Replies:
    4
    Views:
    361
    Dave Thompson
    Feb 7, 2005
Loading...

Share This Page