answersLogoWhite

0

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

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
ReneRene
Change my mind. I dare you.
Chat with Rene
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
More answers

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.

User Avatar

Wiki User

14y ago
User Avatar

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);

User Avatar

Wiki User

13y ago
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