Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Allocation of memory for Array of Pointers

Reply
Thread Tools

Allocation of memory for Array of Pointers

 
 
smartbeginner
Guest
Posts: n/a
 
      12-27-2005
main()
{
int i;
int *a[2];
a=calloc(4,sizeof(*a));
/* The above code I know will not compile[Lvalue required] .But why
cant I allocate space
for all 4 integers i need to store */
for(i=0;i<2;i++)
a[i]=calloc(2,sizeof(*a));
/* this works fine I know?But why not the earlier one */
}

 
Reply With Quote
 
 
 
 
chandan
Guest
Posts: n/a
 
      12-27-2005
Hi,
The reason might be:
You are trying to assign int * to int ** in the frist statement
however, in second stmt you are assigning int * to int * which is a
valid statement.

-Chandan

smartbeginner wrote:
> main()
> {
> int i;
> int *a[2];
> a=calloc(4,sizeof(*a));
> /* The above code I know will not compile[Lvalue required] .But why
> cant I allocate space
> for all 4 integers i need to store */
> for(i=0;i<2;i++)
> a[i]=calloc(2,sizeof(*a));
> /* this works fine I know?But why not the earlier one */
> }


 
Reply With Quote
 
 
 
 
jamesonang@gmail.com
Guest
Posts: n/a
 
      12-27-2005
void *calloc(size_t nelem, size_t elsize)

function calloc return a pointer to a space suitably aligned for
storage of any type of objec.

a[i] is a pointer to a integer , but a is a pointer to a space which is
a pointer to a integer.

 
Reply With Quote
 
moosdau@gmail.com
Guest
Posts: n/a
 
      12-27-2005

smartbeginner wrote:
> main()
> {
> int i;
> int *a[2];
> a=calloc(4,sizeof(*a));
> /* The above code I know will not compile[Lvalue required] .But why
> cant I allocate space
> for all 4 integers i need to store */
> for(i=0;i<2;i++)
> a[i]=calloc(2,sizeof(*a));
> /* this works fine I know?But why not the earlier one */
> }


I think you really should go reading the section6 of comp.lang.c FAQ,
http://c-faq.com/aryptr/index.html
as you'v been suggested.

a is an array, a[i] is a pointer.
they are not the same at all.

 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      12-27-2005
Forgot to mention that u must typecast the calloc function as its
return type is void * (however some compiler automatically return
pointer type of the type_of_its_second_ argument.)

 
Reply With Quote
 
Anand
Guest
Posts: n/a
 
      12-27-2005
chandan wrote:
> Forgot to mention that u must typecast the calloc function as its
> return type is void * (however some compiler automatically return
> pointer type of the type_of_its_second_ argument.)
>

The standard calloc *always* returns void*. And you do not need the
cast. Read through the FAQ and numerous old posts in the archive as to
why you don't need a cast.

You did quote in your previous reply... you forgot here.
FYI: <http://cfaj.freeshell.org/google/>
Also see the welcome note about bottom posting.

--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://c-faq.com/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-27-2005
"chandan" <> writes:
> Forgot to mention that u must typecast the calloc function as its
> return type is void * (however some compiler automatically return
> pointer type of the type_of_its_second_ argument.)


No. calloc() returns a result of type void*, but it's implicitly
converted to the target pointer type. An explicit cast is unnecessary
and can mask errors (such as forgetting the required "#include <stdlib.h>"
or compiling C code with a C++ compiler).

Please read <http://cfaj.freeshell.org/google/>, and don't use silly
abbreviations like "u" for "you".

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-27-2005
chandan wrote:
> Forgot to mention that u must typecast the calloc function as its
> return type is void * (however some compiler automatically return
> pointer type of the type_of_its_second_ argument.)
>


This is simply a pile of manure. Casting the return value from
calloc(), malloc(), or realloc() is unnecessart and serves only to hide
errors.

*All* confoming library implementations will return void * (_never_ the
type of an argument), and that void * pointer will seamlessly be
converted to a pointer to the appropiate type.

Pay no attention to chandon; he knows nothing.
 
Reply With Quote
 
Antonio Contreras
Guest
Posts: n/a
 
      12-27-2005
smartbeginner wrote:
> main()
> {
> int i;
> int *a[2];


The above sentece declares a as an array of two pointers to integers,
which I think it's not what you want.

> a=calloc(4,sizeof(*a));


Now you try to assign a pointer to an array, sth that cannot be done.
As you said an Lvalue is required in the left side of an assignment,
and an array identifier is not an Lvalue.

> /* The above code I know will not compile[Lvalue required] .But why
> cant I allocate space
> for all 4 integers i need to store */
> for(i=0;i<2;i++)
> a[i]=calloc(2,sizeof(*a));
> /* this works fine I know?But why not the earlier one */
> }


I think that what you wanted to do was something along the lines of:

#include <stdlib.h>

int main (void) {
int (*a)[2]; /*Declare a as a pointer to an array of two integers*/
a = malloc(2 * sizeof(*a));
return 0;
}

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      12-27-2005

smartbeginner wrote:
> main()


Implicit typing of main() is no longer supported in the latest
standard, so start using either

int main(void)

or

int main(int argc, char **argv)

> {
> int i;
> int *a[2];
> a=calloc(4,sizeof(*a));
> /* The above code I know will not compile[Lvalue required] .But why
> cant I allocate space
> for all 4 integers i need to store */


You have declared a as a 2-element array of pointers to int; as such,
you don't need to allocate any memory for a itself.

You're getting the error because a is an array type, not a pointer
type, and array type objects are not modifiable.

Secondly, how are you going to store 4 items into a 2-element array?

> for(i=0;i<2;i++)
> a[i]=calloc(2,sizeof(*a));
> /* this works fine I know?But why not the earlier one */
> }


Because each element of a is a pointer type.

It would really help if you would explain exactly what you're trying to
do here. You're obviously confused about something, but without
knowing what you're trying to accomplish, I'm not sure I can help.

It sounds like you're trying to allocate 2 arrays of 2 elements each.
If that is the case, here are a couple of ways to do it:

/* First method */

#include <stdlib.h>

int main(void)
{
int **a;

/*
* Allocate enough memory to hold two int * objects
*/
a = calloc(2, sizeof *a); /* or malloc(2 * sizeof *a); */
if (a)
{
int i;
for (i = 0; i < 2; i++)
{
/*
* Allocate enough memory to hold two int objects
*/
a[i] = calloc(2, sizeof **a); /* or malloc(2 * sizeof **a);
*/
if (a[i])
{
/* assign a[i][0] and a[i][1] */
}
else
{
printf("Memory allocation failed for a[%d]\n", i);
}
}
}
else
{
printf("Memory allocation failed for a\n");
}
return 0;
}

/* Second method */

#include <stdlib.h>

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

a = calloc(2, sizeof *a); /* or malloc(2 * sizeof *a); */
if (a)
{
int i;
for (i = 0; i < 2; i++)
{
/* assign a[i][0] and a[i][1] */
}
}

return 0;
}

 
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
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
problem with pointers in c. and dynamic memory allocation manoharyes@gmail.com C Programming 1 05-23-2006 03:39 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
classes, pointers, vectors, and memory allocation mosfets@gmail.com C++ 2 02-06-2005 07:51 PM
strings, arrays, pointers and dynamic memory allocation swarsa@msn.com C Programming 5 12-24-2004 06:59 PM



Advertisments