Array in c | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Array in c

Hi, How I can get the array size from input?is it possible?

15th Apr 2020, 4:17 AM
Neo_pr78
Neo_pr78 - avatar
4 Réponses
+ 2
Yes declear char arr[90]; int n; Scanf("%d",&n); arr[n];
15th Apr 2020, 4:35 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar
+ 5
Yes it is possible, you can declare array or string size dynamically https://www.sololearn.com/learn/C/2950/?ref=app
15th Apr 2020, 4:49 AM
Ipang
+ 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/
15th Apr 2020, 4:53 AM
Shadow
Shadow - avatar
+ 2
Check this out
15th Apr 2020, 4:36 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar