answersLogoWhite

0


Best Answer

To dynamically allocate an array, use 'void *realloc(void *array, size_t size)', where array is the pointer you plan on reallocating and size is the new length of the array in bytes.

One-dimensional array:

{

int len = 10;

int *array = NULL;

array = (int*)realloc(array, len * sizeof(int));

}

Two-dimensional array:

{

int xlen = 10;

int ylen = 10;

int **array2d = NULL;

array2d = (int**)realloc(array2d, xlen * sizeof(int*));

/* After reallocating array2d, do the same to its contents. */

int index;

for (index = 0; index < xlen; index++) {

array2d[index] = (int*)realloc(array2d[index], ylen * sizeof(int));

}

array2d[5][5] = 24;

/* Clean up. */

for (index = 0; index < xlen; index++) {

free(array2d[index]);

}

free(array2d);

return 0;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

1. First allocate memory for the bigger chunk,

e.g. for the array float **matrix,

do

matrix = malloc(sizeof(n*sizeof(float *));

then

for (i=0;i<n;i++)

matrix[i] = malloc(sizeof(n*sizeof(float));

You can do calloc for allocation with initializing

or,

realloc for increase/decrease in size with reallocation.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Well, sort of. Here is a method:

void **alloc2d (size_t elemsize, size_t N, size_t M)

{

void **p;

int i;

p= malloc (N*sizeof (void *));

for (i= 0; i<N; ++i) p[i]= malloc (M*elemsize);

return p;

}

void free2d (void **p, size_t N, size_t M)

{

int i;

for (i= 0; i<N; ++i) free (p[i]);

free (p);

}

...

int **myarray = (int **)alloc2d (sizeof (int), 12, 23);

...

free2d ((void **)myarray, 12, 23);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How would you dynamically allocate a one-dimensional and two-dimensional array of integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can arrays be created dynamically?

Yes, arrays can be created dynamically. The following shows how it can be done in C: void f (unsigned n) { int* p = malloc (n * sizeof (int)); /* allocate memory to accommodate n integers */ /* use p... */ free (p); p = 0; }


How do you use malloc and calloc to allocate memory for 100 integers?

int *p, *q; p = (int*) malloc (100 * sizeof (int)); q = (int*) calloc (100, sizeof (int)); Note that p is left in an uninitialised state whereas q is initialised with the value zero.


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


What construct can you use to define an array?

For any type T, the type T[n] instantiates an array of n elements of type T, where n is a constant expression. int a[100]; // a fixed-length array of 100 integers (allocated in static memory) void f (const int n) { int b[n]; // a fixed-length array of n integers (allocated on the stack) // ... } If the number of Ts in an array could change at runtime, we can allocate memory on the heap instead: void g (int n) { int* ptr = malloc (n * sizeof(int)); // allocate n integers on the heap ptr[0] = 42; // access memory using suffix notation // ... n *= 2; // double the size of the array ptr = realloc (ptr, n * sizeof (int)); // reallocate (allocation may move to new memory) assert (nptr[0]==42); // assert that existing values are retained even after a reallocation // ... free (ptr); // don't forget to release memory when it is no longer required! }


Write a program in c that prompt user with following lines add two integers test an integer for odd or even and quit?

write a program in C that prompts the user with the following lines: a) Add two integers c) Compare two integers for the larger t) Test an integers for odd or even q) Quit

Related questions

What are the rules in adding integers with unlike signs?

For each pair of such integers, find the difference between the absolute values of the two integers and allocate the sign of the bigger number to it.


Can arrays be created dynamically?

Yes, arrays can be created dynamically. The following shows how it can be done in C: void f (unsigned n) { int* p = malloc (n * sizeof (int)); /* allocate memory to accommodate n integers */ /* use p... */ free (p); p = 0; }


When the negative integers the positive integers and 0 are combined the integers are formed?

Negative integers, zero and the positive integers, together form the set of integers.


Are the fractions 2436 and 1624 equivalent or not?

2436 and 1624 are integers, not fractions. And, as integers, they are unequal.2436 and 1624 are integers, not fractions. And, as integers, they are unequal.2436 and 1624 are integers, not fractions. And, as integers, they are unequal.2436 and 1624 are integers, not fractions. And, as integers, they are unequal.


What are non positive integers?

Non-positive integers are zero and the negative integers.


Which set represent the integers?

The set of integers represents the integers.


9 squared belongs to what family of real numbers?

At least the following families: all integers; all positive integers; all odd integers; and all "square integers", that is, integers that are squares of other integers.


What are the three categories of integers?

The set of integers is divided into three subsets. One is the positive integers. Another is the negative integers. The last subset has one element -- zero. In sum, integers are composed of the positive integers, the negative integers, and zero.


How many integers are there in the numerals?

All the numerals are integers (positive integers)


What are the boundary points of the integers?

The boundary points of the integers is simply the integers.


What is the addition of integers?

the addition of integers is when adding negative and positive integers


How do you use malloc and calloc to allocate memory for 100 integers?

int *p, *q; p = (int*) malloc (100 * sizeof (int)); q = (int*) calloc (100, sizeof (int)); Note that p is left in an uninitialised state whereas q is initialised with the value zero.