How to initialize rows of 2D array of strings in C - c

I want to store strings in a 2D array using pointers but I'm confused with how to do it. The examples I've seen use only arrays of ints or use the brackets[] to allocate a fixed size of memory. So I'm trying to initialize my 2D array of strings and this is what I have:
char ** stringArr = (char**)malloc(/*I don't know what goes here*/);
for(i = 0; i < rows; i++)
stringArr[i] = (char*)malloc(cols *sizeof(char));
As you can see the parameter for my first call of malloc, I am stuck as to what to put there if I want an exact x number of rows, where each row stores a string of chars. Any help would be appreciated!

Do this, because you're allocating some number of pointers:
malloc(rows * sizeof(char*))

You will want to use the number of rows.
char ** stringArr = malloc(rows * sizeof(char*));
Also, do not typecast the return value of a malloc() call.

Use sizeof *ptr * N.
Notice how this method uses the correct type even if stringArr was char ** stringArr or int ** stringArr.
stringArr = malloc(sizeof *stringArr * rows);
for(i = 0; i < rows; i++)
stringArr[i] = malloc(sizeof *(stringArr[i]) * cols);

Related

What is the right way to initialize double pointer in c

As title, I want to know how to initialize double pointer with sizeof a pointer.
For instance
int **p=malloc(sizeof *p * rows);
for(size_t i = 0; i < rows; i++){
p[i]=malloc(sizeof ? * cols);
}
What should I fill in ?.
Any help would be appreciated.
What should I fill in ?.
In general when you have
X = malloc(sizeof ? * NUMBER);
the ? is to be replaced with the type that X points to. That can simply written as *X.
So the line:
p[i]=malloc(sizeof ? * cols);
is to be:
p[i]=malloc(sizeof *p[i] * cols);
Notice that a 2D array can be created much simpler. All you need is
int (*p)[cols] = malloc(sizeof *p * rows);
Here p is a pointer to an array of cols int. Consequently sizeof *p will be the size of an array of cols int.
Using this VLA based technic means that you can allocate the 2D array using a single malloc. Besides making the code more simple (i.e. only 1 malloc) it also ensures that the whole 2D array is in consecutive memory which may give you better cache performance.
It looks like you want p to be an array that can hold pointers, and the number of pointers is rows. So you can allocate memory for p like this:
int ** p = malloc(sizeof(int *) * rows);
Now if you want p[i] to point to an array that holds cols ints, do this:
p[i] = malloc(sizeof(int) * cols);

Using pointer of pointer to reference a matrix [duplicate]

This question already has answers here:
Why can't we use double pointer to represent two dimensional arrays?
(6 answers)
Closed 3 years ago.
I am very new to C, and I am trying to get a int **data to represent a matrix of integers.
I first created an array of arrays, then I tried referencing the pointer of pointer to that array like so:
int **pointer;
int data[num_rows][num_cols];
pointer = (int**) data[0];
However I get a warning: cast to pointer from integer of a different size.
Can someone please help me understand what is going on, and how can I assign an array to int **pointer? (I have to use the double pointer)
The line int data[num_rows][num_cols]; does not actually declare an "array of arrays!" Rather, it declares a single array with two dimensions - in memory, this will be a single block of data, of size num_rows x num_cols x sizeof(int).
To get an "array of arrays" that you can access using a 'double pointer', you generally have to use the malloc function. Something like the following:
int **pointer = malloc(num_rows * sizeof(int*)); // allocate an array of pointers
for (int i = 0; i < num_rows; ++i) {
pointer[i] = malloc(num_cols * sizeof(int)); // allocate each row array
}
You can then access any [row][column] element via the double pointer:
pointer[row][column] = 1234;
When you've finished with the array(s), be sure to free the memory, like this:
for (int i = 0; i < num_cols; ++i) {
free(pointer[i]); // free each row array
}
free(pointer); // free the array of arrays
Alternatively, given your int data[num_rows][num_cols]; you could avoid the malloc calls inside the for loop, as follows:
int **pointer = malloc(num_rows * sizeof(int*)); // allocate an array of pointers
for (int i = 0; i < num_rows; ++i) {
pointer[i] = &data[i][0]; // Assign a pointer to the beginning of each row
}
Feel free to ask for further clarification and/or explanation.

C: Is it possible to automatically create a given number of arrays?

Is it possible in anyway to recursively create a given number of arrays in C with predetermined length? I want to experiment with arrays for a clustering project and would be really practical if i could do this.
Yes it is possible, allocate an array of pointers then allocate all the arrays:
T **array = malloc(rows * sizeof *array);
for (i = 0; i < rows; i++)
{
array[i] = malloc(cols * sizeof **array);
}
it create rows numbers of array. Each array is an array of cols numbers of T.

How to create at runtime a two dimensional array in C

I cannot create a 2D array from 2 variables (eg int arr[i][j] not allowed) so how would I create a dynamically sized 2D array?
The dimensions of the array are only known at runtime in my program. The array is to represent a grid. How would I code this in C?
First allocate an array of pointers.
/* size_x is the width of the array */
int **array = (int**)calloc(size_x, sizeof(int*));
Then allocate each column.
for(int i = 0; i < size_x; i++)
{
/* size_y is the height */
array[i] = (int*)calloc(size_y, sizeof(int));
}
You can access the elements with array[i][j]. Freeing the memory is done in 'reverse' order:
for(int i = 0; i < size_x; i++)
{
free(array[i]);
}
free(array);
You have to allocate a 1-dimensional array:
int* array = calloc(m*n, sizof(int));
And access it like this:
array[i*n + j]
The compiler does exactly this when accessing two-dimensional arrays, and will probably output the same code when n can be guessed at compile time.
This is a FAQ on comp.lang.c (I took the liberty to add the c-faq tag), it even has a FGA (frequently given answer :-)
See http://c-faq.com/aryptr/index.html, 6.16 How can I dynamically allocate a multidimensional array?
In C a multidimensional array is just an array for which each element is another array.
So you need to first allocate memory for one array (the rows). You can use the malloc() function which will return a pointer to the array.
Then you iterate through the array and for each element you allocate memory for the number of columns.
NOTE: don't forget to free the memory you manually allocate with the free() function in the same way you used malloc() to allocate it.
Some of the examples show multiple (more than 2) allocations for the array; it is perfectly feasible to do it in just two allocations (error checking omitted) for an n × m array:
int **array = calloc(m, sizeof(*array));
int *data = calloc(m * n, sizof(*data));
for (int i = 0; i < m; i++)
array[i] = &data[i * n];
...use array[i][j]...
free(array[0]);
free(array);

C Allocating Two Dimensional Arrays

I am trying to allocate a 2D dimension array of File Descriptors... So I would need something like this
fd[0][0]
fd[0][1]
I have coded so far:
void allocateMemory(int row, int col, int ***myPipes){
int i = 0,i2 = 0;
myPipes = (int**)malloc(row * sizeof(int*));
for(i = 0; i < row;i++){
myPipes[i] = (int*)malloc(col * sizeof(int));
}
}
How can I set it all too zeros right now I keep getting a seg fault when I try to assign a value...
Thanks
So, first, you're going to have to pass in a pointer to myPipes:
void allocateMemory(int rows, int cols, int ***myPipes) { ... }
Then it's easy:
*myPipes = malloc(sizeof(int) * rows * cols);
And of course, you'd call it with:
int **somePipes;
allocateMemory(rows, cols, &somePipes);
short answer: change your innermost malloc to a calloc.
long answer provided by the c faq:
http://c-faq.com/~scs/cclass/int/sx9b.html
What you need to understand is that C doesn't really have a way to allocate a true multidimensional array. Instead, you just have a pointer to an array of pointers. Treat your data structure as such and you will be fine.

Resources