473,395 Members | 2,468 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,395 software developers and data experts.

Allocation of memory for Array of Pointers

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 */
}

Dec 27 '05 #1
21 1746
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 */
}


Dec 27 '05 #2
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.

Dec 27 '05 #3

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.

Dec 27 '05 #4
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.)

Dec 27 '05 #5
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/
Dec 27 '05 #6
"chandan" <ch*********@gmail.com> 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) ks***@mib.org <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.
Dec 27 '05 #7
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.
Dec 27 '05 #8
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;
}

Dec 27 '05 #9

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;
}

Dec 27 '05 #10

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 */
}


The First one won't compile because a is an array and is NOT a pointer.

Dec 27 '05 #11
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.)


Please do not top-post, do not use silly confusing abbreviations
(such as "u" and the ilk), and do include context.

In addition your advice is wrong. There is never any need to cast
the return values from calloc, malloc, and friends. There IS a
need to #include the appropriate header, such as stdlib.h.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Dec 27 '05 #12
"Chuck F." wrote:

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.)
Please do not top-post, do not use silly confusing abbreviations
(such as "u" and the ilk), and do include context.

....and do *not* generally be an asshole in any other way. ;-)
In addition your advice is wrong. There is never any need to cast
the return values from calloc, malloc, and friends. There IS a
need to #include the appropriate header, such as stdlib.h.

True, you don't need the cast. But if you are stubborn, the cast
is *not* prohibited by the C standard. ;-)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Dec 27 '05 #13
i 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.)
i am sorry for the silly abbreviations, i had used. Thanks, for the
advice.

Martin Ambuhl wrote:Pay no attention to chandon; he knows nothing.
I never told that i know everything. i am a beginner to C and, really,
i don't know anything, as such, about C. I got my first lesson and I am
eager to learn more.

Keith wrote: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).


In my case, i was trying to compile this code with a C++ compiler. But,
please, can you elaborate how it masks the error and how stdlib.h helps
in conversion of type void * to target pointer type? i mean, how it
determines the target pointer type, if i am using a C compiler.

Dec 28 '05 #14
chandan said:
Keith wrote:
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).
In my case, i was trying to compile this code with a C++ compiler.


That's always a mistake. You wouldn't try to compile Algol with a Pascal
compiler, so why compile C with a C++ compiler?
But,
please, can you elaborate how it masks the error and how stdlib.h helps
It provides a full prototype for calloc, thus telling the compiler that
malloc returns void *.
in conversion of type void * to target pointer type?
By telling the compiler that calloc returns void *, the prototype assists in
the generation of correct code. Without the prototype, the compiler would
be forced to assume that calloc returns int (which calloc doesn't in fact
return).

In the following example, I use malloc rather than calloc, for the simple
reason that experienced C programmers hardly ever use calloc as its side
effect is generally (but not quite always) unnecessary, but the reasoning
is identical.
i mean, how it
determines the target pointer type, if i am using a C compiler.


#include <stdio.h> /* for printf prototype */
#include <stdlib.h> /* for malloc prototype */

typedef int T; /* any object type will do here */

int main(void)
{
T *p; /* compiler now knows p has type T *, so all is well */
size_t n = 42; /* compiler now knows n has type size_t, so all is well */
p = malloc(n * sizeof *p); /* malloc returns void *, and the compiler
* knows that p has type T *, and the
* compiler knows how to convert
* between void * and T *, so there is
* no problem here. */
if(p != NULL)
{
printf("Yes, I got the memory. It's at %p.\n", (void *)p);
free(p);
}

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 28 '05 #15
chandan wrote:
i 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.)

i am sorry for the silly abbreviations, i had used. Thanks, for the
advice.

Martin Ambuhl wrote:
Pay no attention to chandon; he knows nothing.

I never told that i know everything.


*Everything* you wrote was wrong. It is not a question of your knowing
everything: everything you "know" is wrong and you have no business
inflicting your ignorance on others as advice.

Dec 28 '05 #16
Richard wrote:
/* malloc returns void *, and the compiler
* knows that p has type T *, and the
* compiler knows how to convert
* between void * and T *, so there is
* no problem here. */


Richard, i got your point. But, still I have a little confusion.

In section 7.8.5 of K&R, they mention that

The pointer returned by malloc or calloc has the proper alignment for
the object in question, but it must be cast into the appropriate type,
as in
int *ip;
ip = (int *) calloc(n, sizeof(int));

Is the above given statement is no longer valid? And, if it is valid
statement, then which point i am missing, now?

Dec 28 '05 #17
chandan said:
Richard, i got your point. But, still I have a little confusion.

In section 7.8.5 of K&R, they mention that

The pointer returned by malloc or calloc has the proper alignment for
the object in question, but it must be cast into the appropriate type,


Yes, K&R got it wrong. The cast is not required. The following quote is from
Dennis Ritchie's "errata" site:

"142(§6.5, toward the end): The remark about casting the return value of
malloc ("the proper method is to declare ... then explicitly coerce") needs
to be rewritten. The example is correct and works, but the advice is
debatable in the context of the 1988-1989 ANSI/ISO standards. It's not
necessary (given that coercion of void * to ALMOSTANYTYPE * is automatic),
and possibly harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended error. On
the other hand, pre-ANSI, the cast was necessary, and it is in C++ also."

Taken from: <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 28 '05 #18
Martin Ambuhl wrote:
*Everything* you wrote was wrong. It is not a question of your knowing
everything: everything you "know" is wrong and you have no business
inflicting your ignorance on others as advice.
Cool down, man!! Why, you are showing so much aggression?
everything: everything you "know" is wrong.


No, not now.... :-). Feeling happy.

Dec 28 '05 #19
Richard wrote:
Taken from: <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>


Thank you, very much, for the link. :-)

Dec 28 '05 #20
chandan wrote:
Martin Ambuhl wrote:
*Everything* you wrote was wrong. It is not a question of your knowing
everything: everything you "know" is wrong and you have no business
inflicting your ignorance on others as advice.

Cool down, man!! Why, you are showing so much aggression?


Don't patronize me, you idiot. You are the one that not only posted
bullshit but then tried to justify your doing it.
There is little worse than someone who writes something like: 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.)


You tell someone that they *must* do something. What you tell them they
*must* do is not only not required, but bad practice. Further, the
parenthesized sentence is hopelessly confused: it is the library
function that returns a value, not the compiler, and neither the library
function nor the compiler looks at an argument type to decide what to
return.

You pose as someone who knows, and prescibe to others. You have no
right either to claiming knowledge or to prescribing to others. Giving
bullshit advice is a sin; claiming that it *must* be followed is a
mortal sin.
everything: everything you "know" is wrong.


No, not now.... :-). Feeling happy.


Turn you home-schooling psychology certification in for a refund. The
day that a know-nothing bullshitter like you has any business being
patronizing to me is a long way off.

Dec 28 '05 #21
"chandan" <ch*********@gmail.com> writes:
Martin Ambuhl wrote:
*Everything* you wrote was wrong. It is not a question of your knowing
everything: everything you "know" is wrong and you have no business
inflicting your ignorance on others as advice.


Cool down, man!! Why, you are showing so much aggression?
everything: everything you "know" is wrong.


No, not now.... :-). Feeling happy.


At your current level of expertise, you need to acknowledge that your
advice isn't necessarily correct. You've posted misinformation
(unintentionally, I'm sure), and you've phrased it in a way that
implies you're very certain that you're correct. This is positively
dangerous.

If you want to post a guess while making it very clear that it's only
a guess, that's ok; there are plenty of people who can correct you if
you're wrong. (We all make mistakes, after all.) But posting bad
information while claiming certainty is far worse than making an
honest mistake.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Dec 28 '05 #22

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

Similar topics

2
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
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...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because 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...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
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; }
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.