Allocation of memory to a pointer that points an array

R

R.kumaran

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
 
N

Nitin

R.kumaran said:
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
 
I

Irrwahn Grausewitz

(e-mail address removed) (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;
}
 
M

Martin Dickopp

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

#include said:
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
 
R

Robert W Hand

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
 
R

R.kumaran

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
 
K

Kevin Easton

R.kumaran said:
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.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top