Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
but when i access the attributes of Structure i get the exception, the code i used is below

What I have tried:

i have structure name "Guitar", code that is used to create 2 D array of Guitar Objects
Guitar ** guitarlists = (Guitar **)malloc(sizeof(Guitar *));
 for (int i = 0; i < row; i++)
     guitarlists[i] = (Guitar *)malloc(sizeof(Guitar));
Updated 17-May-18 3:25am

Guitar ** guitarlists = (Guitar **)malloc(sizeof(Guitar *));

That should be
Guitar ** guitarlists = (Guitar **)malloc(row * sizeof(Guitar *));

As Thaddeus Jones correctly pointed out, if you need a bidimensional array of Guitar then you have to modify
guitarlists[i] = (Guitar *)malloc(sizeof(Guitar));
as well.

Please note: since you apparently are using C++, why don't you use C++ programming language and standard library features (e.g. containers)?
If you want a 2D array of r rows and c columns, you can do this:

Guitar ** guitarlists = (Guitar **)malloc(r* sizeof(Guitar *));
for (int i = 0; i < r; i++)
	guitarlists[i] = (Guitar *)malloc(c * sizeof(Guitar));
CPallini 17-May-18 7:16am
Damn, I have to update my solution. :-)
In C++ it is standard to use the "new" new and delete operators like that:
Guitar *guitarlists = new Guitar[row];//normal array
delete guitarlists; 
Or am I missing some bits?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900