Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Allocation of memory to a pointer that points an array

Reply
Thread Tools

Allocation of memory to a pointer that points an array

 
 
R.kumaran
Guest
Posts: n/a
 
      09-02-2003
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
 
Reply With Quote
 
 
 
 
Nitin
Guest
Posts: n/a
 
      09-02-2003
"R.kumaran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (R.kumaran) wrote in
<(E-Mail Removed) >:
>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.
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-02-2003
(E-Mail Removed) (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
 
Reply With Quote
 
Robert W Hand
Guest
Posts: n/a
 
      09-02-2003
On 2 Sep 2003 02:34:45 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
R.kumaran
Guest
Posts: n/a
 
      09-03-2003
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
 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      09-03-2003
R.kumaran <(E-Mail Removed)> 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[i][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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Array of pointer to std::vector and memory allocation hapa C++ 4 08-23-2010 03:54 PM
evaluate NULL to a pointer which a CONST pointer points to G C++ 3 01-08-2007 04:22 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM



Advertisments