0

I am writing a Theater program using a dynamic 2D array. To store a booking details I use structure. When I try to initialize one of the variables inside each index of 2D array, I receive unhandled exception error.

Function with error:

void initializing_tickets(ticket **arrayPtr, int row, int col){
int i, j, counter;

for(i = 0; i < row; i++)
{
    for(j = 0; j < col; j++)
    {
        (*(arrayPtr + i) + j) -> id = 0; // debugger explains that expression cannot be evaluated
        printf("%d ", (*(arrayPtr + i) + j) -> id);
    }
    printf("\n");
    } 
} //end of initializing_tickets()

My program so far:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/* Structures */
typedef struct Theater
{
   int id;
   int status;
   char name[20];
   char phone[15];

 } ticket;


 /* Global variables */

 ticket **array2D; // pointer to a 2D array


 /* Constructors */
 void create_Theater(int *row, int* col);
 void test();
 void loop_array(ticket **arrayPtr, int row, int col);
 void initializing_tickets(ticket **arrayPtr, int row, int col);

 int main(void)
 {
    int row = 0, col = 0;
    create_Theater(&row,&col);
    initializing_tickets(array2D, row, col);
    //loop_array(array2D, row, col);
    //test();



   printf("\n\n");
   system("pause");
   return(0);
 }

void create_Theater(int *row, int* col)
{
    int r = 0, c = 0;
    int i;
    assert(row);
    assert(col);

    *row = r;
    *col = c;

    printf("Please enter the row dimensions of the Theater\n");
    scanf("%d", row);

    printf("Please enter the column dimensions of the Theater\n");
    scanf("%d", col);

    array2D = (ticket**)malloc(r*sizeof(ticket*));
    for (i = 0;i<r;i++)
    {
        array2D[i] = (ticket*)malloc(c*sizeof(ticket));
    }

} // end of create_Theater()

void initializing_tickets(ticket **arrayPtr, int row, int col){
int i, j, counter;

for(i = 0; i < row; i++)
{
    for(j = 0; j < col; j++)
    {
        (*(arrayPtr + i) + j) -> id = 0; // debugger explains that expression cannot be evaluated
        printf("%d ", (*(arrayPtr + i) + j) -> id);
    }
    printf("\n");
    } 
} //end of initializing_tickets()

void loop_array(ticket **arrayPtr, int row, int col)
{

    int i, j;

    for(i = 0; i < row; i++)
    {
        printf("Row is ok");
        for(j = 0; j < col; j++)
        {
            printf("Col is ok");
        }
    } 
}

I think array is not allocated properly to a memory, but I can't find a mistake. Thank you for attention!

4
  • array2D looks to be properly allocated in memory. Commented Mar 14, 2016 at 11:25
  • I found 1 mistake! It's in void create_Theater(int row, int col). I did a check with printf("Row: %d\n", r); printf("Col: %d\n", c); and it returned me 0 0, means there is no values stored. And I use those in array2D = (ticket**)malloc(rsizeof(ticket)); . How do I pass dimension values entered by user via pointers in this situation? Commented Mar 14, 2016 at 11:40
  • You should use proper 2D arrays instead of this pointer-to-pointer thing. So much easier to read and maintain. Code like (*(arrayPtr + i) + j) is very questionable. Commented Mar 14, 2016 at 12:08
  • But dynamic memory allocation assumes that you are using a pointers. If I was making static array, then there is no need for pointers. Pointer to pointer is for accessing 2nd dimension of the array. Commented Mar 14, 2016 at 12:15

1 Answer 1

2

Change your function to:

void create_Theater(int* row, int* col){
   int i;
   assert(row);
   assert(col);

   printf("Please enter the row dimensions of the Theater\n");
   scanf("%d", row);

   printf("Please enter the column dimensions of the Theater\n");
   scanf("%d", col);

   array2D = (ticket**)malloc((*row)*sizeof(ticket*));
   for (i = 0;i<(*row);i++)
   {
      array2D[i] = (ticket*)malloc((*col)*sizeof(ticket));
   }
} // end of create_Theater()

It works.

Sign up to request clarification or add additional context in comments.

Comments

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.