0

Okay so I'm trying to pass elements of a 2D array with string elements to a 2D array in struct. I made a code, but it receives a run-time error. I think there's a problem in the code where I'm trying to initialize board2[i][j]. Any help is appreciated.

  char ***board;
  int i, j, size;

  printf("Enter the size of array:");
  scanf("%d", &size);

  if((board = (char***)malloc(sizeof(char**)*size))==NULL)
  {
       printf("Memory Allocation failed\n");
       return -1;
  }

  for(i=0; i<size; i++)
  {
       if((board[i] = (char**)malloc(sizeof(char*)*size))==NULL)       
       { 
          printf("Memory Allocation failed\n");
          return -1;
       }
       for(j=0; j<size; j++)
       {
          if((board[i][j] = (char *)malloc(sizeof(char)*4))==NULL)
          {
            printf("Memory Allocation failed\n");
            return -1;
          }
          a = rand() % 2;  
          b = rand() % 2;
         if(a==0 && b==0)
            strcpy(board[i][j], "ab");
         else if(a && b==0)
            strcpy(board[i][j], "Ab");
         else if(a==0 && b==1)
            strcpy(board[i][j], "aB");
         else
            strcpy(board[i][j], "AB");
        }

  struct data{
    const char *element;
    int visited;
  };    
  void board2_initialize(char ***board, int size)
  {
  struct data **board2;


  for(i=0;i<size;i++)
  {
    for(j=0;j<size;j++)
    {
     board2[i][j].element = board[i][j];
     board2[i][j].visited = 0;
     printf("%s", board2[i][j].element);
    }
  }
  }

EDIT: Forgot to mention that the initialization will occur inside a function

6
  • You're not allocating board2 anywhere. So sure, it will crash. Have you left a part of your code out? Commented Apr 11, 2012 at 14:09
  • I tried allocating board2 the same way as board1 but it receives an error. :/ Commented Apr 11, 2012 at 14:12
  • You never seed rand(), which means it's seeded with 1. This has the consequence of producing the same pattern every time, so it's not random at all. Commented Apr 11, 2012 at 14:27
  • @foo What do you mean by seed rand()? Sorry but I only have 2 months programming experience. :p Commented Apr 11, 2012 at 14:41
  • @BluPixel There is a function called srand() that should be used to seed rand(). It initializes rand with a starting point, typically it's called with time(NULL) as argument as in: srand( time(NULL) );. You only have to do this once. Commented Apr 11, 2012 at 14:55

1 Answer 1

3

You allocate it just the same way you do the board arrays:

struct data **board2 = malloc(sizeof(struct data *) * size);

for(i = 0; i < size; i++)
{
    board2[i] = malloc(sizeof(struct data) * size);

    for(j = 0; j < size; j++)
    {
        /* ... */
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Will this work if I tried to initialize board2 in a function? I tried it but it receives an error. "invalid conversion from void* to data**" .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.