Allocation of memory to a pointer that points an array

Discussion in 'C Programming' started by R.kumaran, Sep 2, 2003.

  1. R.kumaran

    R.kumaran Guest

    hello all
    I have a problem in allocating memory for a pointer to an array.
    this is my code

    void main()
    {
    int(*a)[20];

    *a=(int *)malloc(2*2*sizeof(int));
    ........
    .........
    .........

    }

    when compiled, the compiler reports with an error Lvalue required..

    pls help me out to solve this problem

    thanks in advance
    R.kumaran
     
    R.kumaran, Sep 2, 2003
    #1
    1. Advertising

  2. R.kumaran

    Nitin Guest

    "R.kumaran" <> wrote in message
    news:...
    > hello all
    > I have a problem in allocating memory for a pointer to an

    array.
    > this is my code
    >
    > void main()
    > {
    > int(*a)[20];
    >
    > *a=(int *)malloc(2*2*sizeof(int));
    > .......
    > ........
    > ........
    >
    > }
    >
    > when compiled, the compiler reports with an error Lvalue required..
    >
    > pls help me out to solve this problem
    >
    > thanks in advance
    > R.kumaran


    *a is the base address of the array of 20 pointers to int. This can't be
    changed.
    I don't know what do you want to achive by malloc here.

    thanks,
    ..nitin
     
    Nitin, Sep 2, 2003
    #2
    1. Advertising

  3. (R.kumaran) wrote in
    <>:
    >hello all
    > I have a problem in allocating memory for a pointer to an array.
    >this is my code
    >
    >void main()
    >{
    >int(*a)[20];
    >
    >*a=(int *)malloc(2*2*sizeof(int));

    <SNIP>

    Try:

    /****** EXAMPLE 1 ******/
    #include <stdio.h>

    int main( void )
    {
    int a[20]; /* a is array of 20 ints */

    int (*pa)[20] = &a; /* pa is a pointer to array of 20 ints,
    ** initialized to point to array a
    */
    int i;

    for ( i = 0; i < 20; i++ )
    {
    (*pa)[ i ] = i;
    printf( "%d\n", a[ i ] );
    }
    return 0;
    }

    There's no need to malloc() anything at all!

    Or, if you have to use malloc, try:

    /****** EXAMPLE 2 ******/
    #include <stdio.h>
    #include <stdlib.h>

    int main( void )
    {
    int *pi; /* a pointer to int */
    pi = malloc( 20 * sizeof(int) );

    int i;

    for ( i = 0; i < 20; i++ )
    {
    pi[ i ] = i;
    printf( "%d\n", pi[ i ] );
    }
    return 0;
    }

    --
    Air is water with holes in it.
     
    Irrwahn Grausewitz, Sep 2, 2003
    #3
  4. (R.kumaran) writes:

    > hello all
    > I have a problem in allocating memory for a pointer to an array.
    > this is my code


    #include <stdlib.h>

    > void main()


    int main (void)

    > {
    > int(*a)[20];
    >
    > *a=(int *)malloc(2*2*sizeof(int));

    ^^^^^^^
    The cast is not needed, and in this case even wrong, since `a' is not a
    pointer to `int'.

    I don't understand what the `2*2*sizeof(int)' is supposed to mean. What's
    the goal you're trying to achieve?

    > when compiled, the compiler reports with an error Lvalue required..


    Since `a' is a pointer to an array, `*a', i.e. `a' dereferenced, has type
    array of `int'. You cannot assign to an array.

    Unlike what the compiler seems to think, `*a' /is/ an lvalue. However, it is
    not a /modifiable/ lvalue, therefore you cannot assign to it.

    Martin
     
    Martin Dickopp, Sep 2, 2003
    #4
  5. On 2 Sep 2003 02:34:45 -0700, (R.kumaran)
    wrote:

    >hello all
    > I have a problem in allocating memory for a pointer to an array.
    >this is my code
    >
    >void main()


    int main(void)

    >{
    >int(*a)[20];
    >
    >*a=(int *)malloc(2*2*sizeof(int));


    Are you trying to do this?

    int(*a)[20];
    a=malloc(sizeof *a);
    (*a)[2] = 5;

    a is the pointer to an array of 20 ints. *a is not going to be a
    modifiable lvalue. You do not need a cast on malloc. Use the sizeof
    operator to simplify your code. The code for using a is a little more
    complicated than you might want.

    Best wishes,

    Bob
     
    Robert W Hand, Sep 2, 2003
    #5
  6. R.kumaran

    R.kumaran Guest

    Hello
    I thank you all.

    i want to know how to implement a two-dimensional array with help of a
    pointer to an one-dimensional array.

    i read in one book that a two dimensinal array can be defined as

    #define rows 2
    #define cols 2

    void main()
    {
    int (*a)[20]; /* defines an pointer to an array*/

    /*To allocate memory*/

    *a=(int * )malloc(rows*cols*sizeof(int));

    they have accessed each an every element in the two dimensional array
    by
    inside the foe loop


    scanf("%d",(*(a+rows)+cols));

    to print the values again in for loop

    printf("%d",*(*(a+rows)+cols));



    }


    here my problem is the allocation of memory to the pointer to an
    array.

    i think now i have given you a clear pict of my problem

    guide me out

    R.kumaran
     
    R.kumaran, Sep 3, 2003
    #6
  7. R.kumaran

    Kevin Easton Guest

    R.kumaran <> wrote:
    > Hello
    > I thank you all.
    >
    > i want to know how to implement a two-dimensional array with help of a
    > pointer to an one-dimensional array.
    >
    > i read in one book that a two dimensinal array can be defined as
    >
    > #define rows 2
    > #define cols 2
    >
    > void main()
    > {
    > int (*a)[20]; /* defines an pointer to an array*/
    >
    > /*To allocate memory*/
    >
    > *a=(int * )malloc(rows*cols*sizeof(int));


    You are confusing two different ways of allocating two-dimensional
    arrays.

    #define ROWS 5
    #define COLS 10

    int main()
    {
    int (*a)[COLS];
    int *b;

    a = malloc(ROWS * sizeof *a); /* First method */
    b = malloc(ROWS * COLS * sizeof *b); /* Second method */

    a[j] = 1; /* Access element at row i, column j */
    b[i * COLS + j] = 1; /* Acecss element at row i, column j */

    free(a);
    free(b);

    return 0;
    }

    The first method is better, but it only works if the number of columns is
    known at compile-time. If the number of both rows and columns is known
    at compile-time, you can use the even better method:

    int c[ROWS][COLS];

    - Kevin.
     
    Kevin Easton, Sep 3, 2003
    #7
    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. Andrew

    Test if pointer points to allocated memory

    Andrew, Jan 21, 2004, in forum: C Programming
    Replies:
    34
    Views:
    949
    Daniel Haude
    Jan 26, 2004
  2. Ken
    Replies:
    24
    Views:
    3,904
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  4. G
    Replies:
    3
    Views:
    449
    Sylvester Hesp
    Jan 8, 2007
  5. hapa
    Replies:
    4
    Views:
    1,030
    Juha Nieminen
    Aug 23, 2010
Loading...

Share This Page