answersLogoWhite

0


Best Answer

You use a 2-D array with malloc the same way you use any other structure or scalar with malloc. The malloc library call takes a single argument of type size_t (in bytes) and returns a void* pointer to a region of memory that is suitably aligned for any supported data type. An example using the 2-D array...

int *myArray[10][20];

myArray = malloc (sizeof (myArray));

if (myArray == NULL) {...exception processing...};

Note that a 2-D array is really the same as a 1-D array - its a linear region of memory - its just that the compiler does address arithmetic for you.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use 2-D array with malloc?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is really meant by malloc and where you will use this malloc?

malloc is a function of the standard c library (stdlib) and it is abbreviation for memory allocate. What this function does is allocates memory in the RAM of computer to store variable data in it. You will use it whenever you need a place to store you temporary data such as an array or structure. To use malloc all you have to do is call malloc and tell it the size of the memory you want. It will then return a pointer to that memory. persumabely if it fails it returns NULL.


How do you declare an N-Dimensional array using Malloc?

#include <stdlib.h> int **array1 = malloc(nrows * sizeof(int *)); for(i = 0; i < nrows; i++) array1[i] = malloc(ncolumns * sizeof(int));


Difference between 1D array and 2D arrays?

1d array contains single row and multiple columns and 2d array contains multiple row and multiple columns. 2d array is a collection of 1d array placed one below another,while 1d array is simple a collection of elements.


What is a malloc function in C programming with example?

The malloc() function is part of a class of functions that deal with the allocation of memory on the heap. int *a = malloc (sizeof (int) * 100); /* allocate 100 int's */ if (a == NULL) {...} /* deal with possible malloc failure */ /* use a, either as pointer or as array of 100 ints */ free (a); /* release memory back to the library */


How can we create an unsized array to c programme?

Use a pointer... int a*; a = malloc(sizeof(int)*100); //allocate space for 100 elements free(a); a = malloc(sizeof(int)*1000); // allocate space for 1000 elements free(a);


Flow chart of multiplication of 2d array?

algorithm & flowchrt of 2d matrices


How do you free two dimensional dynamic array?

Depends on how you allocated it: every malloc has to to have a corresponding free.


Is there any overhead issues if you make a 2D array arr28 rather than making a 1D array arr16 of size 16 Any memory issues or execution differences between the two?

2D array of size 2x8 and 1D array of size 16


How do you use dynamic memory allocation in getting a array?

Its simple void main() { int *arr[10]; arr=(int*)malloc(sizeof(int)*10); ...... ...... ...... ...... free(arr); }


Subtraction of 2d array in java?

looda le lo mera


Can you delete array?

If the array was allocated with new, then delete it with delete []. Otherwise, if it was allocated with malloc() then delete it with free. Otherwise, you cannot delete it because it was pre-allocated at link-load time by the compiler.


How do you pass a 2D array in a functions?

if you were to call a function you would write it as: function(array[][], int pretend, double pretend2); arrays will always be passed by reference, not by value.