2D dynamic array in C with pointer and without []

Hello,

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include<stdlib.h>

int main()
{
	int n, m;
	scanf_s("%d%d", &n, &m);

	int** my = (int*)malloc(sizeof(int*) * n);
	for (int i = 0; i < n; i++)
	{
		*(my + i) = (int*)malloc(sizeof(int) * m);
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			*(*(my + i) + j) = i + j;
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			printf("%d ", *(*(my + i) + j));
		}
		printf("\n");
	}

	return 0;
}
does this help?

9) int** my = (int**)malloc(sizeof(int**) * n);

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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):

1
2
3
const size_t pSize =sizeof(*int);
...
int**my = calloc(rows, pSize);


Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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

Last edited on
Registered users can post here. Sign in or register to post.