I'd like to create 2D dynamic array in C with pointer and without [].
I wrote the below code in Visual Studio 2019 in Windows 10. But I have many warnings.
Could you please help me solve warnings?
Severity Code Description Project File Line Suppression State
Warning C4047 'initializing': 'int **' differs in levels of indirection from 'int *' C_Project02 C:\Users\My\source\repos\C_Project02\C_Project02\C_Project0e2.c 9
Severity Code Description Project File Line Suppression State
Warning C6011 Dereferencing NULL pointer '*(my+i)+j'. C_Project02 C:\Users\My\source\repos\C_Project02\C_Project02\C_Project0e2.c 19
Severity Code Description Project File Line Suppression State
Warning C6011 Dereferencing NULL pointer 'my+i'. C_Project02 C:\Users\My\source\repos\C_Project02\C_Project02\C_Project0e2.c 12
1. You're writing C, so there's no need for the malloc casts.
2. If you always write your mallocs in the form p = malloc(sizeof(*p)*howmany);
then you don't have to play 'guess the type' in the sizeof expression.
With both of these, the variable type is in exactly one place (as it should be). Everything else flows naturally with the compiler doing all the work.
3. Better variable names. Anything other than loop variables should have a meaningful name.
i and j are fine for matrix loops variables (that's the mathematical notation IIRC), but personally I prefer r and c. Coupled with rows and cols as the respective dimensions, a mistake like for ( r = 0 ; r < cols is going to stick out like a sore thumb.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows, cols;
scanf("%d%d", &rows, &cols);
int** my = malloc(sizeof(*my) * rows);
for (int r = 0; r < rows; r++)
{
// Ignoring my own advice, because sizeof(*(*(my + r))) looks silly.
// Besides, the run-time value of r doesn't matter, it's all about the type,
// so we reduce the specimen sizeof(*(*(my + 0))) to just **my
*(my + r) = malloc(sizeof(**my) * cols);
}
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
*(*(my + r) + c) = r + c;
}
}
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
printf("%d ", *(*(my + r) + c));
}
printf("\n");
}
return 0;
}
A point to note. A pointer is always the same size - irrespective as to what it points. So a pointer to int is the same size as a pointer to pointer to pointer to pointer to double. Also, there is calloc() in which you specify separately the size of an object and the number of objects. You can have say (as C):
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rw, cl;
puts("Enter number rows cols: ");
scanf_s("%d%d", &rw, &cl);
int** arr = calloc(rw, sizeof(int*));
for (int r = 0; r < rw; ++r)
*(arr + r) = calloc(cl, sizeof(int));
for (int r = 0; r < rw; ++r)
for (int c = 0; c < cl; ++c)
*(*(arr + r) + c) = r + c;
for (int r = 0; r < rw; ++r) {
for (int c = 0; c < cl; ++c)
printf("%d ", *(*(arr + r) + c));
puts("");
}
return 0;
}
@salem c. L15. Should be malloc(sizeof(int) * cols); As you need here to allocate storage for the type of object being stored - which in this case in an int.
Enter number rows cols:
3 4
0 1 2 3
1 2 3 4
2 3 4 5