Dynamic Allocation for 2 dimensional Array

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Dynamic Allocation for 2 dimensional Array

Post by taj79 »

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?
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

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;
}
is this what you are looking for?

Regards,
Suman.

P.S: Untested code...so be careful, was hacked up in a hurry :D
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post by jakabjr »

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 :wink: ?
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
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

jakabjr wrote:ok, a few questions about this:
is there a reason for using (2==t)
Heard about best practices?This is a case in point.You can easily screw up
the whole thing if you miss one character when put in a condition as in:

Code: Select all

while( (t=2) ...) 
the compiler doen't catch this and you go nuts over one damn
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.
jakabjr wrote: and for(...;++i) ...and for(...;i++)? I've seen this used, don't really know why, originality :wink: ?
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.
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)
can u use sizeof without ()?
Yes, sizeof is an operator, not a function.See K&R.
suggestions:
using typecast before malloc gets rid of compiler warnings;
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.
BTW: standard C, and conforming compilers will never ever tell you to do that, trust me.
the string in scanf "%d %d" should be "%d%d" (without space), which is redundant, not necesarry there, since %d consumes all blanks;
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 well :D)...so let each be happy with his own idiosyncrasies.
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.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

sumankar wrote:the assignment operator returns true always
That's not obviously not true. The assignmet operator returns the value that has been assigned. So a = b = 0, equivalent to a = (b = 0) (assignment is right associative), assigns 0 to both a and b. Note that (b = 0) returned 0 (false).
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa »

sumankar wrote:

Code: Select all

while( (scanf(...)=2) ...) 
the assignment operator returns true always, AFAIR, the compiler doen't catch this and you go nuts over one damn
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.
Someone else pointed out already that an assignment may return false...
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.
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

Ooops!my mistake - both of you are right...I was thinking about the success of the operation rather than the actual value returned.
Post Reply

Return to “C”