473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Allocation of memory to a pointer that points an array

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
Nov 13 '05 #1
6 4376
"R.kumaran" <sk*****@rediffmail.com> wrote in message
news:8c**************************@posting.google.c 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
Nov 13 '05 #2
sk*****@rediffmail.com (R.kumaran) wrote in
<8c**************************@posting.google.com >:
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.
Nov 13 '05 #3
sk*****@rediffmail.com (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
Nov 13 '05 #4
On 2 Sep 2003 02:34:45 -0700, sk*****@rediffmail.com (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
Nov 13 '05 #5
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
Nov 13 '05 #6
R.kumaran <sk*****@rediffmail.com> 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.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
10
by: goose | last post by:
Hello all I've written a wrapper for malloc and friends. Its available from http://www.lelanthran.com/downloads/os_mem/index.php The reason for doing writing this so that newbies can...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
15
by: raghu | last post by:
int x; static int y; int *p; int main(void) { int i; int j=9; int *ptr; char *str = "GOOGLE"; static int a;
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.