+ 2
Array in c
Hi, How I can get the array size from input?is it possible?
4 Réponses
+ 2
Yes declear
char arr[90];
int n;
Scanf("%d",&n);
arr[n];
+ 5
Yes it is possible, you can declare array or string size dynamically
https://www.sololearn.com/learn/C/2950/?ref=app
+ 3
Yes, it is, and C offers two ways for this.
The first are so-called variable-length arrays, which are all arrays which size is not declared through an integer constant expression, i.e.
size_t size;
scanf( "%zu", &size );
int arr[ size ];
The advantage is that the array has scope lifetime and is automatically handled for you, so you don't have to worry about cleaning it up afterwards. However, there are also some drawbacks, e.g. VMA's may not be structure members. For a more detailed overview, visit:
https://en.cppreference.com/w/c/language/array
The other option is to dynamically allocate the memory yourself, i.e.
size_t size;
scanf( "%zu", &size );
int* arr = malloc( size * sizeof( int ) );
...
free( arr );
This is more pointer-driven and you need to make sure to free the memory after using it, hence it may be harder to use at first, but has a lot of advantages over VMA's, e.g. lifetime over different scopes or resizability. You can find an example here:
https://www.sololearn.com/learn/C/2947/
+ 2
Check this out