Hi,
     Check this pice of code for dynamic memory allocation for twodimensional 
array

#include <stdio.h>

float **bidimen_alloc(int row, int col);

main()
{
        int i;
        float **ptr = NULL;

        ptr = bidimen_alloc(10 ,10);
        if (ptr == NULL) {
                printf("ERROR\n");
                exit(1);
        }
        ptr[0][0] = 10.20;
        ptr[9][9] = 100.20;
        printf("%f %f\n", ptr[0][0], ptr[9][9]);
}

float **bidimen_alloc(int row, int col)
{
        int i = 0;
        float **mem = NULL;

        mem = (float **)malloc(row * sizeof( float*));

        if (mem == NULL)
                return NULL;
        for (i = 0; i < row; i++) {
                mem[i] = (float *)malloc(col * sizeof(float));
                if (mem[i] == NULL) {
                        while (--i > 0)
                                free(mem[i]);
                        return NULL;
                }
        }
        return mem;
}

 

Thanks & Regards,

Rajath N R

--- On Mon, 10/20/08, Miguel Caro <[EMAIL PROTECTED]> wrote:
From: Miguel Caro <[EMAIL PROTECTED]>
Subject: [c-prog] Please help again, bidimensional dynamic array as a global 
variable
To: [email protected]
Date: Monday, October 20, 2008, 6:41 PM










    
            

Hello everybody,(Hello Tyler)



Well, some days ago, i was asking about how to define a bidimensional

dynamic array as a global variable to use as incoming variable in a

function, but i could not solve the problem.



Let us see , for example in a part of a programm my problem:



#include<iostream>

...

using namespace std;



float a[10][10];// in this place i wish define a bidimenional dynamic array

agian: how to do it?

...

//follow the function 



float suma(int k, float a[][10])

 {

   float s=0;

   for (int i=1;i<k;i++)

     for (int j =1;j<k;j++)

       s=s+a[i][j];

   return (s);

}



int main()

{ 

  ...

  suma(k,a);

...

}



But instead defining an static bidimensional array a[10][10], to use after

as an incoming variable in a function, i wish to define it as dynamic

array(to work with  a huge array for example(a[500] [500]) )



would you like to help me?

-- 

View this message in context: http://www.nabble. com/Please- help-again% 
2C-bidimensional -dynamic- array-as- a-global- variable- tp20082005p20082 
005.html

Sent from the C-prog mailing list archive at Nabble.com.




      

    
    
        
         
        
        








        


        
        


      

[Non-text portions of this message have been removed]

Reply via email to