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 ?

Odko
  • 151
  • 1
  • 1
  • 8
  • 5
    Declare a global pointer to char, and then `malloc()` or `calloc()` the storage. – JohnH May 06 '15 at 02: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. – David C. Rankin May 06 '15 at 02:22
  • How many dimensions the array has is not particularly relevant - N dimensions just means you have to allocate an N-dimensional array. – Iskar Jarak May 06 '15 at 02:30
  • You are thinking of dynamic memory allocation. As @JohnH has suggested, use `malloc()`, `calloc()` and `realloc()` family of dynamic memory allocating functions. – alvits May 06 '15 at 02: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 ? – Odko May 06 '15 at 14:29

2 Answers2

1

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.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Thanks for the answer. What if an array is 2D array ? – Odko May 06 '15 at 02:31
  • @akiD Then dynamically allocate each entry in the second dimension using a loop. Try searching for other questions about that. – Iskar Jarak May 06 '15 at 02:32
  • 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](http://stackoverflow.com/questions/17477234/difference-b-w-allocating-memory-to-2d-array-in-1-go-or-row-by-row) – Paul Rooney May 06 '15 at 02:34
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.

joshbooks
  • 489
  • 3
  • 8
  • 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.. – JohnH May 06 '15 at 02:20
  • Thanks for the answer. What if an array is 2D array ? – Odko May 06 '15 at 02:29