|
-
Oct 6th, 2001, 02:14 PM
#1
Thread Starter
PowerPoster
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?
-
Oct 6th, 2001, 04:41 PM
#2
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;
}
-
Oct 10th, 2001, 07:04 PM
#3
Hyperactive Member
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...
Designer/Programmer of the Comtech Operating System(CTOS)
-
Oct 10th, 2001, 07:12 PM
#4
Thread Starter
PowerPoster
Thanks guys. It makes sense now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|