-4
#include <stdio.h>
#include <stdlib.h>

int** asd(int map[][7]);

int main(void)
{
    int **child_map = (int**)malloc(sizeof(int*) * 6);
    int i, k;
    for (i = 0; i < 6; i++)
        child_map[i] = (int*)malloc(sizeof(int) * 7);


    for (i = 0; i < 6; i++)
    {
        for (k = 0; k < 7; k++)
        {
            child_map[i][k] = 10;
        }
    }

    for (i = 0; i < 6; i++)
    {
        for (k = 0; k < 7; k++)
        {
            printf("%3d", child_map[i][k]);
        }
        printf("\n");
    }

    int **map = NULL;
    map = asd(child_map);

    for (i = 0; i < 6; i++)
    {
        for (k = 0; k < 7; k++)
        {
            printf("%3d", map[i][k]);
        }
        printf("\n");
    }

    return 0;
}

int** asd(int map[][7])
{
    int **a = (int**)malloc(sizeof(int*) * 6);
    int i, k;
    for (i = 0; i < 6; i++)
        a[i] = (int*)malloc(sizeof(int) * 7);


    for (i = 0; i < 6; i++)
    {
        for (k = 0; k < 7; k++)
        {
            a[i][k] = map[i][k];
        }
    }
    return a;
}

In function, a[i][k] and map[i][k] are not mapped. Value of array a is very strange like 99882978. Why doesn't it work?
I have been made tree of game like go and each tree node has copy of parent's map. So i made function malloc two dimesional array , copies map of parent and return pointer of copy map. But it do not work... I'm very stuck here...

S w
  • 1
  • 1

1 Answers1

2

child_map is an array of pointers. The declaration int map[][7] is for a 2-dimensional array of int, not an array of pointers. You need to change asd to:

int **asd(int **map)
Barmar
  • 741,623
  • 53
  • 500
  • 612