-
Changing the array size
Is there any way to resize a 1 or 2 dimensional array.
Let us I have:
myarray[5][10]
How can I make it:
myarray[7][15]
after I have declared it and I have also set the values in it?
And also will it delete all the values in the array after I resize it?
-
try realloc like this:
Code:
void main( void )
{
long *buffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );
buffer[99] = 1;
printf("%s %ld\n","before realloc buffer[99]=",buffer[99]);
/* Reallocate and show new size: */
if( (buffer = (long *)realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );
printf("%s %ld\n","after realloc buffer[99]=",buffer[99]);
free( buffer );
return;
}
-
Just do this:
int myarray[5][10];
In a function do this:
free(myarray); // gets rid of it...
int myarray[7][17]; //makes the array again
// This should do it
Doing this will erase the variable's contents...
I don't think that you can do it Jim's way if you need 2 dimensional arrays... If you absolutely need 2D arrays without using my function and without getting rid of the contents, you need assembler as you can do ABSOLUTELY ANYTHING with assembler...
-
Thanks guys. It makes sense now:)