Reputation: 39
How I can write a C code which has a global array that takes its length from main() as a user input?
I tried so many different ways but none of them worked so far, one for example:
#include <stdio.h>
const int length;
int array[length];
int main()
{
scanf("%d",&length);
}
Upvotes: 1
Views: 4232
Reputation: 1791
You need to create a dynamic array using malloc to solve the problem, as example shown in the following.
#include<stdio.h>
#include<stdlib.h>
int *array;
main()
{
int length;
scanf("%d",&length);
array = (int *)malloc (length*sizeof(int));
// array[0] = 100;
}
I am assuming your array length fit in the range of integer, or change accordingly.
Upvotes: -1
Reputation: 123448
Arrays declared at file scope (outside the body of any function) have static
storage duration, meaning the space for them is allocated as soon as the program starts, before any statements have been executed. Because of this, their sizes must be known at compile time.
If you want to allocate memory that's globally visible, but not sized until runtime, you'll have to use dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>
int *array; // personally, I would name these something like
size_t length; // g_array and g_length to emphasize their "global-ness"
// and disambiguate them from any local variables.
int main( void )
{
// get length value somehow
array = malloc( sizeof *array * length );
if ( !array )
{
// Could not allocate memory, handle as appropriate
}
// do stuff with array
free( array );
return 0;
}
Upvotes: 0
Reputation: 3872
Referring to your code, I can point to this:
int array[length];
This won't work of course because, at the compile time, the size of the array
is unknown. You can't define an array with indefinite (From Compiler's perspective during compilation) size statically!
If you want to decide the size of array run-time (i.e. dynamically), you can't do that by means of declaring a variable the way you have done.
Also, using const
with length
won't work here. Get rid of const
.
What you can and should do is, take an int
pointer, and get the starting address of the memory that gets dynamically allocated.
You can use malloc
function for that.
I won't write the whole code for you, but it should contain which looks something similar to this:
int *ptr = malloc(length * sizeof(int));
From here, if user has entered 10 (i.e. length
= 10) for example, and if malloc
doesn't return NULL
, then you should be able to store upto 10 integers starting from address stored in ptr
pointer, and of course access them by incrementing the pointer ptr
.
If you read through any of the manual pages for malloc
you should be able to deal with what header file you need. I am not writing what file you need, because I want you to read the man page and code for yourself.
Upvotes: 7
Reputation: 399763
You obviously can't. When length
gets its value, do you expect the memory allocation to magically happen backwards in time, retroactively?
That's just not how C works, it's not that abstract.
You have to make the array just a pointer (which you can then declare extern
where needed), and have the length as a separate variable, also extern
.
Upvotes: 3