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;
}
Chat with our AI personalities
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.
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);
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; }
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.
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;
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 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