i m doing like this
#define MAXROWS 100
float *arr[MAXROWS];
//read no of rows & cols from user
for(i=0=i<rows;i++)
arr = (float *)malloc(col * sizeof(float));
Here I can have rows at the max MAXROWS.
Is it possible to have dynamic allocation of memory without knowing/declaring the maximum no of rows?
Dynamic Allocation for 2 dimensional Array
Moderator: Board moderators
Code: Select all
#include <stdio.h>
#include <stdlib.h> /* for malloc */
typedef int Element;
int main(void)
{
Element **ppa;
int rows, cols, i;
while ( printf("Enter #rows and #cols::"), (2 == scanf("%d %d", &rows, &cols)))
{
ppa = malloc(rows * (sizeof *ppa));
for ( i = 0; i < rows; ++i )
{
ppa[i] = malloc(cols * (sizeof *ppa[i]));
}
/* whatever you want to do with this...*/
for ( i = 0; i < rows; ++i )
{
free(ppa[i]);
}
free(ppa);
}
return 0;
}
Regards,
Suman.
P.S: Untested code...so be careful, was hacked up in a hurry

ok, a few questions about this:
is there a reason for using (2==scanf(...)) and for(...;++i) instead of (scanf(...)==2) and for(...;i++)? I've seen this used, don't really know why, originality
?
can u use sizeof without ()?
suggestions:
using typecast before malloc gets rid of compiler warnings;
the string in scanf "%d %d" should be "%d%d" (without space), which is redundant, not necesarry there, since %d consumes all blanks;
is there a reason for using (2==scanf(...)) and for(...;++i) instead of (scanf(...)==2) and for(...;i++)? I've seen this used, don't really know why, originality

can u use sizeof without ()?
suggestions:
using typecast before malloc gets rid of compiler warnings;
the string in scanf "%d %d" should be "%d%d" (without space), which is redundant, not necesarry there, since %d consumes all blanks;
Understanding a problem in a natural way will lead to a natural solution
Heard about best practices?This is a case in point.You can easily screw upjakabjr wrote:ok, a few questions about this:
is there a reason for using (2==t)
the whole thing if you miss one character when put in a condition as in:
Code: Select all
while( (t=2) ...)
stupid bug!OTOH, put 2 in the LHS, and do the test, here 2, a numeric literal can never be a lvalue(an address, that is, for most cases)and the compiler cribs.
For most purposes its a matter of style, in C.Though there are(or used to be) some compiler/system pair where the former could be directly translated to a single machine instruction, and the latter could not be.jakabjr wrote: and for(...;++i) ...and for(...;i++)? I've seen this used, don't really know why, originality?
It makes a difference in C++, when its overloaded for some object, where the postfix takes more time because it requires some extra ops.
But that's really off-topic here(in other words, I'll leave that as an exercise for you to find out!Google, google and google more)
Yes, sizeof is an operator, not a function.See K&R.can u use sizeof without ()?
Yes.But that's flirting with danger.You don't want the compiler to shut up, rather scream at you.That's the compiler's job.So make it a little easy on the poor chap.Also malloc returns a void *, which due to C's inherent implicit-type-conversion-black-magic-for-void can be converted to almost anything without your forcing it down its neck.suggestions:
using typecast before malloc gets rid of compiler warnings;
BTW: standard C, and conforming compilers will never ever tell you to do that, trust me.
Yes, but that is a style issue, some people like me, think its more readable, some think its more confusing(and/or the other group doesn't know his C as wellthe string in scanf "%d %d" should be "%d%d" (without space), which is redundant, not necesarry there, since %d consumes all blanks;

Actually putting spaces helps me count faster how many I've put in when the input is supposed to read in some 4, 7, 8 items at a time - without my specs, of course.
That is one heck of a post!Flames welcome,
Suman.
Last edited by sumankar on Thu May 26, 2005 3:11 pm, edited 2 times in total.
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Someone else pointed out already that an assignment may return false...sumankar wrote:the assignment operator returns true always, AFAIR, the compiler doen't catch this and you go nuts over one damnCode: Select all
while( (scanf(...)=2) ...)
stupid bug!OTOH, put 2 in the LHS, and do the test, here 2, a numeric literal can never be a lvalue(an address, that is, for most cases)and the compiler cribs.
Anyway, scanf(...) isn't a left value so the compiler will reject it in exactly the same way

Ciao!!!
Claudio
Last edited by CDiMa on Thu May 26, 2005 3:09 pm, edited 2 times in total.