Originally Posted by
kliro89
But lets say that the size of the array is not know.
Call it x, the user will actually give x, then I have to use a code like
Code:
#include <stdlib.h>
main(){
int x;
x=10;
int A[x]={0};
}
Why do I get this, one must be able to define an array even though you as the programmer don't know the size?
Generally what you need to do is wait till the user enters the x value before sizing your array...
On c-99 compliant compilers:
Code:
int main (void) // Note: This is the correct form for main!
{ int x;
puts ("Enter the array size please : ");
scanf("%d", &x);
int array[x];
// do stuff
return 0;
}
On pre-C99 compilers:
Code:
int main (void)
{ int x;
int *array;
puts ("Enter the array size please : ");
scanf ("%d",&x);
array = calloc(x,sizeof(int));
// do what you need here
free(array);
return 0;
}
It can't allocate memory for the the array until the size is known.
If I define an Array to be to size 10. I am still able to pick out values from A[12], A[13] etc., why is this?
Have I dont something wrong? Shouldnt I get an error-message that it is out of the size of the array?
No you haven't... However; C does not include range checking, so you have to do this by yourself.
It is up to you as a programmer to always stay within array bounds with your code.