0

Hello I am new to C and I need someone to explain concepts to me. I am a JAVA programmer and I am trying to write a program in C. My current issue is trying to initialize an array with an unknown number. I know in C an array has to be initialized with a number instead of a variable like you can in Java. My question is if I can do this in Java:

int i = 0;
char array [i];

void f(){
\\some code
i++;
}

How can I do this in C? I'm trying to fill an array with certain strings that I get from a file. I don't know how many I will be getting from the file however. I have tried reading about malloc but in one tutorial it says:

int *pointer;
pointer=malloc(2*sizeof(int));

is equivalent to

int array[2];

But I'm looking for a way to do this while increment the array.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • When you `malloc` a zone, you'll need to `free` it later (unlike in Java which has a GC). Read some good C programming book. – Basile Starynkevitch Feb 17 '15 at 19:49
  • 2
    I think you'd need to go back and rework this question. It's not very clear exactly what you want to do, as written. – Jonathan Wood Feb 17 '15 at 19:50
  • 3
    They are far from equivalent. Delete the tutorial from your brain and blacklist the website you found it on. –  Feb 17 '15 at 19:51
  • http://www.cplusplus.com/reference/cstdlib/realloc/ this, and you might want to read a few pages about pointers. Great ur stepping away from java and trying some real programming!! – Simon Feb 17 '15 at 20:05

2 Answers2

2

First to mention, malloc() and family is used for dynamic (runtime) memory allocation whereas int arr[2] usually denotes compile time memory allocation. They are not exactly equivalent.

However, if you want to resize the allocated memory on-the-fly, you're on right track. What you need to do next is to use realloc() to re-size the previously allocated memory location.

You can read the man page for more details.

Also, while using dynamic memory in C, you need to keep in mid that there is no garbage collector in C. You need to free() up every bit of memory allocated by you.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

I know in C an array has to be initialized with a number instead of a variable like you can in Java
In C99 and beyond, variable initiated arrays are available.

My current issue is trying to initialize an array with an unknown number.
and:
But I'm looking for a way to do this while increment the array.

If you have an unknown number of elements at run-time, you can write a function to create (and free) memory, passing the relevant arguments as you need them. Here is an example of a function to create (and free) a 2 dimensional array of ints:

int ** Create2Dint(int **arr, int cols, int rows)
{   
    int space = cols*rows; 
    int    y;

    arr   = calloc(space, sizeof(int));
    for(y=0;y<cols;y++)
    {
        arr[y] = calloc(rows, sizeof(int)); 
    }
    return arr;
}  

void free2DInt(int **arr, int cols)
{
    int i;
    for(i=0;i<cols; i++)
        if(arr[i]) free(arr[i]);
    free(arr);  
}    

If, during execution, you need to change the allocation of memory (change the size of the array) you can use realloc(), implemented here in similar fashion:

int ** Realloc2D(int **arr, int cols, int rows)
{   
    int space = cols*rows; 
    int    y;

    arr   = realloc(arr, space*sizeof(int));
    for(y=0;y<cols;y++)
    {
        arr[y] = calloc(rows, sizeof(int)); 
    }
    return arr;
}   

Usage example:
(execute with two integer command line arguments, both > 0)

int main(int argc, char *argv[])
{
    int **array = {0};
    int cols, rows;

    cols = atoi(argv[1]);
    rows = atoi(argv[2]);

    array = Create2Dint(array, cols, rows);
    //Do stuff here to use array
    //Memory requirements change during runtime:
    cols += 20;
    rows += 50;

    array = Realloc2D(array, cols, rows);  
    //use array again...

    //When you are finished with the memory, free it: 
    free2DInt(array, cols);

    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87