My question is same as my topic how to i rediclare a array ....
thx for the future help
My question is same as my topic how to i rediclare a array ....
thx for the future help
If you have not statically declared the array, you'll be able to realloc() the pointer.
See the malloc(), realloc(), and free() man pages.
It seems that you don't know much about arrays to begin with, so somebody trying to explain it to you for the first time would take a while. I suggest that you visit: http://www.cprogramming.com/tutorial/c/lesson8.html
http://www.exforsys.com/content/view/1995/364/
The first link just touches arrays, but the second link should explain things a little better for you. After reading these post back with any questions that you may have.
Good Luck
S0n1C!
I know what a array is because I am good at vb.net in that I can just say redim nameofarray(size) I just don't know how to do it in c I will read the tutorial thx
Well, you can't. Perhaps that is the reason you got the last reply.Originally Posted by Demon.killer
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
"Well, you can't. Perhaps that is the reason you got the last reply."
?????
What u mean i guess what I ment was resize a array (redeclaring a array)
I read the tutorial it doesn't give me what I want
any help would be better :lol:
You can't resize or redeclare a static array. You need to use dynamic allocation.Originally Posted by Demon.killerOriginally Posted by Kennedy
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
ohhhhh okay thx
This one way.
Code:#include <stdio.h> #include <stdlib.h> int main() { int *array; /* the "array" */ int size = 50; /* its size */ /* * Dynamically allocate the array. */ array = malloc(size * sizeof *array); if ( array != NULL ) { int *temp; /* for use with resizing */ /* * Do stuff. */ array[size - 1] = size; printf("array[%d] = %d\n", size - 1, array[size - 1]); /* * Resize the "array". */ size = 100; temp = realloc(array, size * sizeof *array); if ( temp != NULL ) { array = temp; /* successfully changed size */ } else { free(array); /* failed - free original memory */ /* exit or whatever (don't accidentally free it a second time later on) */ } /* * Do stuff. */ array[size - 1] = size; printf("array[%d] = %d\n", size - 1, array[size - 1]); /* * Clean up. */ free(array); } return 0; } /* my output array[49] = 50 array[99] = 100 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*