2

Maybe this question is answered somewhere but i can't find it. I want to declare global array. But the size of this array depends on my input.How do i do that ?, Thank you

Idea is:

char* array[maxsize];

int main(){
    int maxsize;
    scanf("%d",&maxsize);
}

EDIT: What if an array is 2D array ?

5
  • 5
    Declare a global pointer to char, and then malloc() or calloc() the storage. Commented May 6, 2015 at 2:09
  • There is nothing wrong with the global declaration so long as you #define maxsize 128 (or some value) above. But, understand you are creating an array of pointers to char. it is probably better to declare a pointer and allocate in main or another function if your intent is a global string. Commented May 6, 2015 at 2:22
  • How many dimensions the array has is not particularly relevant - N dimensions just means you have to allocate an N-dimensional array. Commented May 6, 2015 at 2:30
  • You are thinking of dynamic memory allocation. As @JohnH has suggested, use malloc(), calloc() and realloc() family of dynamic memory allocating functions. Commented May 6, 2015 at 2:30
  • Thank you for your answers. As I understand calloc function allocates at heap, and global variables allocated at data or bss segment, right ? So where is this method allocating memory ? Commented May 6, 2015 at 14:29

2 Answers 2

2

Use calloc like so

#include <stdio.h>
#include <stdlib.h>

char* array=NULL;

int main()
{
    int maxsize;
    scanf("%d",&maxsize);

    array = calloc(maxsize, sizeof(char));

    free(array);
    array = NULL;
}

This dynamically allocates maxsize chars on the applications heap. Note that a call to free is required to release the dynamic allocation. If this is not done it's called a memory leak. In this trivial program though its not too serious if free is not called.

Ok so technically its not an array its a pointer but the two are mostly interchangeable. Using calloc for a char array is a good idea, as all values are initialised to 0 and if you copy some string in there its already zero terminated.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. What if an array is 2D array ?
@akiD Then dynamically allocate each entry in the second dimension using a loop. Try searching for other questions about that.
Then you have a few options. You can allocate an array of pointers, then loop through them and allocate the second level items or just do one big allocation that you know is big enough and calculate offsets into that array for each element. See this
0

What you're talking about is known as dynamic allocation and it's done a little differently to the normal allocation (which happens is a different part of memory), to do this in C you use a function from stdlib.h (remember to #include it) called calloc which takes two arguments the number of elements and the size of each element, so assuming you want an array of chars the code would look something like this:

char *array;

int main(void)
{
  int maxsize;
  scanf("%d", &maxsize);
  array = calloc(maxsize, sizeof(char));
}

you'll notice that there is no [] after the declaration of array, that's because it is not an array but rather a pointer, but no fear you can still access indices like an array. So array[1] will still work provided you have at least 2 elements in the array.

2 Comments

This is because, in C, array notation decays to pointer math... a[5] is equivalent to *(a + 5). With that in mind, you may not be fully surprised to learn that 5[a] is also valid C syntax to get the 5th element of an array, as it decays to *(5 + a) which is the same result -- even though it is very bad form..
Thanks for the answer. What if an array is 2D array ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.