I am trying to create a C struct with a member that points to a 2d array. The compiler generates error messages, and I am unable to find out the root cause. I am basically trying to access a struct member to obtain the value of an element in a 3*2 array.

Any help would be appreciated.

// THIS IS TYPEDEF OF STRUCTURE


typedef struct coord_struct
            {               

               unsigned char  *(*coord_ptr)[];   //[B]ATTEMPT TO CREATE POINTER TO 2D ARRAY[/B]
                
            } coord_test;
// DECLARATION OF VARIABLE
 
coord_test  *test_ptr, coord_test_program ;
// ASSIGN ARRAY ELEMENTS

 unsigned char  f_coord[3][2]   = {{0 , 24} , {0 , 34} , {0 , 44}}; //[B]ATTEMPT TO FILL IN ARRAY[/B]
// INITIALIZE STRUCTURE

coord_test coord_test_program = 
{       
    .coord_ptr               = &f_coord // ASSIGN ARRAY NAME TO POINTER
};
// MAIN PROGRAM

int main
{
unsigned char array_val;
	
    test_ptr = 	&coord_test_program;
	
	
    array_val =  *(*(test_ptr ->  coord_ptr + ROW) + COL);
	
}

No one posted a reply to my question, but I figured it out and decided to post the answer in case someone else in the future needs a solution to the same problem.

I will post the correct replacements on a one to one basis:

// THIS IS TYPEDEF OF STRUCTURE


typedef struct coord_struct
            {               

               unsigned char  (*coord_ptr)[2];   //ATTEMPT TO CREATE POINTER TO 2D ARRAY
                
            } coord_test;
// ASSIGN ARRAY ELEMENTS
	

 unsigned char  f_coord[3][2]   = {{0 , 24} , {0 , 34} , {0 , 44}}; //ATTEMPT TO FILL IN ARRAY
// INITIALIZE STRUCTURE
     
    coord_test coord_test_program =
    {
    .coord_ptr = f_coord // ASSIGN ARRAY NAME TO POINTER
    };
// DECLARATION OF VARIABLES

coord_test coord_test_program, *test_ptr;
// INVOCATION IN MAIN PROGRAM

int main()
{

   unsigned char   array_val;	

  test_ptr = &coord_test_program;
	
// ACCESS ELEMENT FROM SPECIFIED ROW AND COLUMN
	
    array_val = *(*(test_ptr -> coord_ptr + ROW)+ COL);
}

I hope this helps some other hapless soul trying to get a simple answer to a simple question

Congo man
Your program is working fine..!!

thank you :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.