I am trying to copy a 2d character array into another 2d character array using the string function strcpy but it's giving me the error of access violation. I don't know what is it that I am doing wrong. I am posting the code and error can somebody tell me what is it that I am doing wrong
int searching(char *name[],char * namesearched,int size)
{
int count =0;
int start = 0;
int end = count;
for(;start<=end;)
{
int mid = (start + end)/2;
if(strcmp(namesearched,name[mid])==0)
{
return mid;
}
else if(strcmp(namesearched,name[mid])==1)
{
end=mid -1;
}
else if(strcmp(namesearched,name[mid])==-1)
{
start = mid +1;
}
}
return -1;
}
void sorting(char **name,char ** meaning,int count)
{
for (int i=0;i<count;i++)
{
for(int j=i+1; j<count; j++)
{
char tempname[100];
char tempmeaning[100];
if(strcmp(name[j-1],name[j])>0)
{
strcpy(tempname,name[j]);
//strcpy(name[j],tempname);
strcpy(name[j-1],name[j]);
strcpy(name[j],name[j-1]);
strcpy(name[j-1],tempname);
strcpy(tempmeaning,meaning[j]);
strcpy(meaning[j],meaning[j-1]);
strcpy(meaning[j-1], tempmeaning);
}
}
}
}
void main()
{
int size=60;
int count=0;
char namesearched[100];
cout << "Enter the name to be searched: ";
cin.getline(namesearched , 100);
char** name= new char * [size];
char** meaning = new char * [size];
for(int i=0;i < size ; i++)
{
name[i]= new char [100];
meaning[i]= new char[100];
count ++;
}
name[0] = "Journalist";
name[1] = "Blister";
name[2] = "List";
name[3] = "Listen";
name[4] = "Novelist";
name[5] = "Song";
name[6] = "Eat";
name[7] = "West";
name[8] = "Idealist";
name[9] = "Industry";
name[10] = "Legalist";
name[11] = "Write";
name[12] = "Medal";
name[13] = "Nation";
name[14] = "Accident";
name[15] = "Nest";
name[16] = "Bird";
name[17] = "Animal";
name[18] = "Lion";//wrong
name[19] = "Pigeon";
name[20] = "Real";
name[21] = "Accept";
name[22] = "Ability";
name[23] = "Bald";
name[24] = "Backbite";
name[25] = "Wakeful";
name[26] = "Absolute";
name[27] = "Wail";
name[28] = "Abiding";
name[29] = "Unacceptable";
name[30] = "Tacker";
name[31] = "Vain";//wrong
name[32] = "Abolish";
name[33] = "Taking";
name[34] = "Unarmed";
name[35] = "Habit";
name[36] = "Notus";
name[37] = "Impecle";
name[38] = "Accelerate";
name[39] = "Agony";
name[40] = "Sulk";
name[41] = "Nowise";
name[42] = "Hypocrisy";
name[43] = "Nape";
name[44] = "Eccentric";
name[45] = "Naturally";
name[46] = "Gratitude";
name[47] = "Mesmerizing";
name[48] = "Epic";
name[49] = "Abstain";
name[50] = "Enactment";
name[51] = "Hammock";
name[52] = "Nodal";
name[53] = "Laborious";
name[54] = "Nonverbal";
name[55] = "Haggle";
name[56] = "Notorious";
name[57] = "Lagger";
name[58] = "Pathetic";
name[59] = "Norms";
meaning[0] = "Sahaafi";
meaning[1] = "Chaala";
meaning[2] = "Fehrist";
meaning[3] = "Sunna";
meaning[4] = "Naval Nigaar";
meaning[5] = "Ganna";
meaning[6] = "Khanna";
meaning[7] = "Maghrib";
meaning[8] = "Tadawuri";
meaning[9] = "Sannat";
meaning[10] = "Zabta Parast";
meaning[11] = "Likhna";
meaning[12] = "Tangha";
meaning[13] = "Qoom";
meaning[14] = "Hadsa";
meaning[15] = "Ghonsla";
meaning[16] = "Parinda";
meaning[17] = "Janwar";
meaning[18] = "Shair";
meaning[19] = "Kabootar";
meaning[20] = "Haqeekat";
meaning[21] = "Qabool";
meaning[22] = "Kabliyat";
meaning[23] = "Ganja";
meaning[24] = "Ghebat Karna";
meaning[25] = "Jagta";
meaning[26] = "Bikul";
meaning[27] = "Gham Karna";
meaning[28] = "Mustakil";
meaning[29] = "NaGawar";
meaning[30] = "Jorna Wala";
meaning[31] = "Gari";
meaning[32] = "Rad kar dena";
meaning[33] = "Dil-chasp";
meaning[34] = "Nehatta";
meaning[35] = "Addat";
meaning[36] = "Dakni hawwa";
meaning[37] = "Rokna";
meaning[38] = "Taiz karna";
meaning[39] = "Sakht Takleef";
meaning[40] = "Roth Jana";
meaning[41] = "Hargiz Nahi";
meaning[42] = "Naffaq";
meaning[43] = "Mankaa";
meaning[44] = "Sanki";
meaning[45] = "Fitratan";
meaning[46] = "Tashakur";
meaning[47] = "Mashoor Karna";
meaning[48] = "Razmiya";
meaning[49] = "Baaz Rakhna";
meaning[50] = "Nifaaz";
meaning[51] = "Jholay ki tarhan ka Bichona";
meaning[52] = "Gutheela";
meaning[53] = "Mehnat Talab";
meaning[54] = "Ghair Lafzey";
meaning[55] = "Takrar Karna";
meaning[56] = "Badnam";
meaning[57] = "Ahista Chalnay walla";
meaning[58] = "Intehai afsoos naak baat";
meaning[59] = "Mayar";
int mid;
sorting( name , meaning , count);
int mid = searching(name,namesearched,count);
if( mid == -1 )
{
char ** tempname = new char* [60];
char ** tempmeaning = new char*[60];
if(count == size)
{
int increase =(10 * size)/100;
size = increase + size;
for(int i=0 ; i<size ; i++)
{
tempname[i] = new char [100];
tempmeaning[i]= new char [100];
}
for(int i = 0; i<count ; i++)
{
strcpy(tempname[i],name[i]);
strcpy(tempmeaning[i],meaning[i]);
}
}
strcpy(tempname[count] , namesearched);
cin >> tempmeaning[count];
count ++;
sorting( tempname , tempmeaning , count);
for (int i =0;i < count ;i++)
{
delete [] name[i];
delete [] meaning[i];
}
//delete [] name;
//delete [] meaning;
name = tempmeaning;
meaning = tempmeaning;
tempmeaning = NULL ;
tempname = NULL;
}
else
{
cout <<"The meaning of " << namesearched << " is: " << meaning[mid] << endl;
}
_getch();
}
Access violation writing location 0x001fbe5c.
The value of count and size is 60
One thing more strcpy works on this line strcpy(tempname , name[j]) but when it encounter this line strcpy(name[j] , name[j-1]) it throws me the error of access violation
This function declaration
void sorting(char *name[],char *meaning[],int size,int count);
does not deal with two-dimensional arrays.
For example if you have two-dimensional arrays like this
char name[60][100];
char meaning[60][100];
then the function declaration will look like
void sorting( char name[60][100], char meaning[60][100], size_t count );
or
void sorting( char name[][100], char meaning[][100], size_t count );
or
void sorting( char ( *name )[100], char ( *meaning )[100], size_t count );
and the value of the argument for the third parameter should be equal to 60.
As for your function declaration then for example this parameter char *name[]
has type of incomplete one-dimensional array of pointers of type char * that is adjusted to type char **. And if the corresponding argument is an array of pointers to string literals then the function has undefined behavior because you may not change string literals.
So it seems you are processing the arrays incorrectly that is their definitions do not correspond to the logic of the function code.
Also parameter size is not used in the function.
Thus your code simply wrong initially.
Take into account that the condition in this if statement
if(strcmp(name[j-1],name[j]))
should look either like
if ( strcmp( name[j-1], name[j] ) > 0 )
if you want to sort the arrays in the ascending order or like
if ( strcmp( name[j-1], name[j] ) < 0 )
if you want to sort the arrays in the descending order.
EDIT: After you appended your question then it is seen that 1) there are memory leaks because the pointers that initially pointed to the allocated memory are reassigned with addresses of string literals and 2) you are trying to change string literals that are immutable.
Instead of for example
name[0] = "Journalist";
you have to write
strcpy( name[0], "Journalist" );
You don't need 2d char arrays, just array of strings. So you can do it without using strcpy. Something like:
void sorting(char *name[],char *meaning[], int count)
{
for (int i = 0; i < count; i++)
{
for(int j = 1; j < count - i; j++)
{
char *tempname;
char *tempmeaning;
if(strcmp(name[j-1],name[j]) > 0)
{
tempname = name[j];
name[j] = name[j-1];
name[j-1] = tempname;
tempmeaning = meaning[j];
meaning[j] = meaning[j-1];
meaning[j-1] = tempmeaning;
}
}
}
}
char *name[] is an array of pointers to char. Pointer to char can be interpreted like a pointer to the first element of array of chars (to string). So if you want to swap two strings in array, you just need to swap pointers to that strings.
Related
i'm trying to turn a 1d array of strings into a 2d array of chars using:
'''''''''''''''
variables
'''''''''''''''
const int width = 20;
const int height = 20;
char arena[width][height];
string arenaline[height];
'''''''''''''''
setup
'''''''''''''''
arenaline[1] = "####################";
arenaline[2] = "#..................#";
arenaline[3] = "#..................#";
arenaline[4] = "###...###..###...###";
arenaline[5] = "#.......#..#.......#";
arenaline[6] = "###...###..###...###";
arenaline[7] = "#..................#";
arenaline[8] = "###...###..###...###";
arenaline[9] = "#.......#..#.......#";
arenaline[10] = "#########..#########";
arenaline[11] = "###..............###";
arenaline[12] = "###.#####..#####.###";
arenaline[13] = "###.####....####.###";
arenaline[14] = "###.####....####.###";
arenaline[15] = "#.....###..###.....#";
arenaline[16] = "#.....###..###.....#";
arenaline[17] = "#..#..###..###..#..#";
arenaline[18] = "#.....###..###.....#";
arenaline[19] = "#.....###..###.....#";
arenaline[20] = "####################";
'''''''''''''''
conversion
'''''''''''''''
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
arena[j][i] = arenaline[i].substr(j,1);
}
}
I want it to convert from the substring to a char so I can use it in an array.
I can't use a string instead of chars because it breaks the function where the array is output to the console.
substr returns a string. string is not implicitly converted to char for "single character" strings.
The correct way to adress single characters of strings is string::operator[] or string::at().
My problem is that the variable 'scoreA' should be the accumulation of all the values generated during each iteration of the for loop. However, I am not getting the accumulated value and get only the value at the final iteration. Could someone Kindly help me out as I am new to C++. I have defined scoreA outside the for loop so my understanding is that the value of scoreA should be the accumulated value for all the iterations. But my guess is that it is being reassigned to 0 because of which it is unable to accumulate the value.
#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string,int> Score_Scheme;
string StringA = "aaaa";
int Score_Compute(string StringA);
int Base_case_A(int lenA);
int main ()
{
Score_Scheme["ac"] = -1;
Score_Scheme["ag"] = -2;
Score_Scheme["at"] = -1;
Score_Scheme["a-"] = -3;
Score_Scheme["cg"] = -3;
Score_Scheme["ct"] = -2;
Score_Scheme["c-"] = -4;
Score_Scheme["gt"] = -2;
Score_Scheme["g-"] = -2;
Score_Scheme["t-"] = -1;
Score_Scheme["--"] = 0;
Score_Scheme["aa"] = 5;
Score_Scheme["cc"] = 5;
Score_Scheme["gg"] = 5;
Score_Scheme["tt"] = 5;
Score_Scheme["ca"] = -1;
Score_Scheme["ga"] = -2;
Score_Scheme["ta"] = -1;
Score_Scheme["-a"] = -3;
Score_Scheme["gc"] = -3;
Score_Scheme["tc"] = -2;
Score_Scheme["-c"] = -4;
Score_Scheme["tg"] = -2;
Score_Scheme["-g"] = -2;
Score_Scheme["-t"] = -1;
int len_StringA = StringA.size ();
cout<<"length of A is:"<<len_StringA<<"\n";
Base_case_A(len_StringA);
return 0;
}
int Base_case_A(int lenA)
{
int scoreA = 0;
for(int i = 0;i<lenA;i++)
{
char x[2];
x[0] = StringA[i];
x[1] = '-';
scoreA=scoreA+Score_Compute(x);
cout<<i<<":"<<scoreA<<"\n";
}
cout<<scoreA<<"\n";
return 0;
}
int Score_Compute(string stringA)
{
return Score_Scheme[stringA];
}
Score_Compute() takes a std::string as input, but when the loop calls Score_Compute(x), x is a char[2] containing 2 char values neither of which are a null terminator.
When a char[] is passed as-is to a std::string, the char[] decays to char*, which std::string then assumes to be null-terminated, which yours is not.
So, the std::string that Score_Compute() receives will be wrong, usually containing random data after the characters you did specify (assuming the code doesn't just crash altogether while searching for a null terminator that doesn't exist), and so it will not match any entries in your std::map, causing Score_Compute() to return 0.
To fix the logic, you need to change the declaration of x to char x[3]; and then add x[2] = '\0'; before converting to std::string:
char x[3]; // <-- fix this!
x[0] = StringA[i];
x[1] = '-';
x[2] = '\0'; // <-- add this!
Otherwise, you have to specify the correct length when converting to std::string:
Score_Compute(string(x, 2));
Alternatively, simply change x to be a std::string to begin with:
string x(2, '\0');
x[0] = StringA[i];
x[1] = '-';
Or:
string x;
s.resize(2);
x[0] = StringA[i];
x[1] = '-';
Or:
string x = " -";
x[0] = StringA[i];
example code:
const char* list[] = {"Elem_E", "Elem_T", "Elem_R", "Turtle", "Rabbit"};
const char ** patterns=0;
.
.
.
bool sec_run = false;
patterns = list;
process_data(patterns, sec_run);
process_data function:
process_data(const char **& pattern, bool sec_run){
.
.
some_variable=0;
for(int i; i < num_patterns;++i){
if(!sec_run){
some_variable = *pattern[i];
}
else{
if(/* list element contains "_" */)continue;
some_variable= /*letter after "_" */
}
if(some_variable == 'E') multiplier = 0;
else if(some_variable == 'T') multiplier = 1;
else if(some_variable == 'R') multiplier = 2;
}
}
So there is the base of what I'm trying to do. I cannot change signature for process_data. To start i do not get how some_variable = *pattern[i]; returns E,T, or R, and I cannot figure out how to iteratively access the full elements in the list. ie "Elem_E" to check for underscore and parse off the E.
I have little background in C++, but have used C numerous times. I am having a difficult time finding visual representation for char **& to help with based understanding of what is going on here, if you can point in the direction of a good tutorial with visual that will also suffice.
Sorry for confusion, forgot quotes in the list.
In C++, reading a parameter passed by reference (with the &) works the same as reading a parameter passed by value (without the &). The difference happens when you assign to the parameter. If the parameter was passed by value then the assignment is only visible inside the function but if it was passed by reference the assignment will be visible outside.
int mynumber = 0;
void foo(int &x)
{
printf("%d\n", x); //prints 0;
x = 10;
}
int main()
{
foo(mynumber);
printf("%d\n", mynumber); // prints 10
}
The equivalent to this in plain C would be to make the x parameter into a pointer and add the required *s and &s:
int mynumber = 0;
void foo(int *x)
{
printf("%d\n", *x);
*x = 10;
}
int main()
{
foo(&mynumber);
printf("%d\n", mynumber); // prints 10
}
Coming back to your code, I don't really know how to solve all your problems (what does the constant Elem_E mean? Is your list NULL terminated or is there a length stored somewhere?) but what I can say is that as long as you don't want to change the patterns global variable from inside process_data, using a char **& will be the same as using a char **.
I don't know how some_variable and multiplier will be used, but I made these changes to calculate them for each string in the list. The variable sec_run is not required in this approach. If no match is found, some_variable and multiplier are set to default values of '\0' and -1.
Output:
item=Elem_E some_variable=E multiplier=0
item=Elem_T some_variable=T multiplier=1
item=Elem_R some_variable=R multiplier=2
item=Turtle some_variable= multiplier=-1
item=Rabbit some_variable= multiplier=-1
Code:
void process_data(const char **& pattern, int num_patterns)
{
const char * item;
for (int i = 0; i < num_patterns; ++i)
{
item = pattern[i];
if ( item == NULL ) continue;
char some_variable = '\0'; // set to default for no match
int multiplier = -1; // set to default for no match
int len = strlen(item);
for (int j = 0; j < len; ++j)
{
if (item[j] == '_' && j + 1 < len)
some_variable = item[j + 1]; /*letter after "_" */
}
if (some_variable == 'E') multiplier = 0;
else if (some_variable == 'T') multiplier = 1;
else if (some_variable == 'R') multiplier = 2;
cout << "item=" << item << " some_variable=" << some_variable << " multiplier=" << multiplier << endl;
}
}
void pattern_test()
{
const char* list[] = { "Elem_E", "Elem_T", "Elem_R", "Turtle", "Rabbit" };
const char ** patterns = list;
// trick to calculate array length
// length of entire array divided by length of one element
int num_length = sizeof(list) / sizeof(list[0]);
process_data(patterns, num_length);
}
The Code needs to write some values in a file by going to the specific adresses with seekp. But when i start it only the first Array is written into the file.
The new adresses can be read from the INI like the new values but its just not going to be written in the file. Its for a games save file.
Code:
const int noq = 5;
const int noqd = 4;
int questData[noqd] = {10, 10, 10, 10};
int adresslist[noqd] ={0x0,0x0,0x0, 0x0};
fstream aus("quest1", ios::binary | ios::out | ios::in);
char buffer[512];
int i;
int j;
int Money, Prize, HRpoints, Map;
int Mappointer, Moneypointer, PrizePointer, HRpointer;
for (j=0; j <= noq; j++)
{
sprintf_s(buffer,"%d",j);
Money = GetPrivateProfileInt(buffer,"RewardMoney", 0, ".\\Settings.ini");
Prize = GetPrivateProfileInt(buffer,"Questprize", 0 , ".\\Settings.ini");
HRpoints = GetPrivateProfileInt(buffer,"RewardHRpoints", 0 , ".\\Settings.ini");
Map = GetPrivateProfileInt(buffer,"Map", 1 , ".\\Settings.ini");
questData[0] = Money;
questData[1] = HRpoints;
questData[2] = Prize;
questData[3] = Map;
Moneypointer = GetPrivateProfileInt(buffer,"RewardMoney", 0, ".\\Pointers.ini");
PrizePointer = GetPrivateProfileInt(buffer,"Questprize", 0 , ".\\Pointers.ini");
HRpointer = GetPrivateProfileInt(buffer,"RewardHRpoints", 0 , ".\\Pointers.ini");
Mappointer = GetPrivateProfileInt(buffer,"Map", 1 , ".\\Pointers.ini");
adresslist[0] = Moneypointer;
adresslist[1] = HRpointer;
adresslist[2] = PrizePointer;
adresslist[3] = Mappointer;
for (i=0; i<=noqd; i++)
{
printf("\n<Adress>\n");
aus.seekp(0x0);
aus.seekp(adresslist[i], ios::beg);
printf("\n<Write>");
aus.write(reinterpret_cast<char*>(&questData[i]), sizeof(questData[i]));
Beep(1000,100);
}
i = 0;
}
aus.close();
}
Your for loops are wrong.
for (i=0; i<=noqd; i++)
This goes 0,1,2,3,4. but your array only goes from 0 to 3.
I am making a 3D application where a boat has to drive through buoy tracks. I also need to store the tracks in groups or "layouts". The buoys class is basically a list of "buoy layouts" inside of which is a list of "buoy tracks", inside of which is a list of buoys.
I checked the local variable watcher and all memory allocations in the constructor appear to work. Later when the calculateCoordinates function is called it enters a for loop. On the first iteration of the for loop the functions pointer is used and works fine, but then on this line
ctMain[j+1][1] = 0;
the function pointers are set to NULL. I am guessing it has something to with the structs not being allocated or addressed correctly. I am not sure what to do from here. Maybe I am not understanding how malloc is working.
Update
I replaced the M3DVector3d main_track with double ** main_track, thinking maybe malloc is not handling the typedefs correctly. But I am getting the same error when trying to access the main_track variable later in calculateCoordinates.
Update
It ended up being memory corruption caused by accessing a pointer wrong in the line
rotatePointD(&(cTrack->main_track[j]), rotation);
It only led to an error later when I tried to access it.
// Buoys.h
////////////////////////////////////////////
struct buoy_layout_t;
struct buoy_track_t;
typedef double M3DVector3d[3];
class Buoys {
public:
Buoys();
struct buoy_layout_t ** buoyLayouts;
int nTrackLayouts;
int currentLayoutID;
void calculateCoordinates();
};
struct buoy_track_t {
int nMain, nYellow, nDistract;
M3DVector3d * main_track,
yellow_buoys,
distraction_buoys;
double (*f)(double x);
double (*fp)(double x);
double thickness;
M3DVector3d start, end;
};
struct buoy_layout_t {
int nTracks;
buoy_track_t ** tracks;
};
// Buoys.cpp
/////////////////////////////
// polynomial and its derivative, for shape of track
double buoyfun1(double x) {return (1.0/292.0)*x*(x-12.0)*(x-24.0);}
double buoyfun1d(double x) {return (1.0/292.0)*((3.0*pow(x,2))-(72.0*x)+288.0);}
// ... rest of buoy shape functions go here ...
Buoys::Buoys() {
struct buoy_layout_t * cLayout;
struct buoy_track_t * cTrack;
nTrackLayouts = 1;
buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(*buoyLayouts));
for (int i = 0; i < nTrackLayouts; i++) {
buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(*(buoyLayouts[0])));
}
currentLayoutID = 0;
// ** Layout 1 **
cLayout = buoyLayouts[0];
cLayout->nTracks = 1;
cLayout->tracks = (buoy_track_t **) malloc(sizeof(*(cLayout->tracks)));
for (int i = 0; i < 1; i++) {
cLayout->tracks[i] = (buoy_track_t *) malloc (sizeof(*(cLayout->tracks)));
}
cTrack = cLayout->tracks[0];
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
cTrack->nMain = 30;
cTrack->f = buoyfun1;
cTrack->fp = buoyfun1d;
cTrack->thickness = 5.5;
cTrack->start[0] = 0; cTrack->start[1] = 0; cTrack->start[2] = 0;
cTrack->end[0] = 30; cTrack->end[1] = 0; cTrack->end[2] = -19;
// ... initialize rest of layouts here ...
// ** Layout 2 **
// ** Layout 3 **
// ...
// ** Layout N **
calculateCoordinates();
}
void Buoys::calculateCoordinates()
{
int i, j;
buoy_layout_t * cLayout = buoyLayouts[0];
for (i = 0; i < (cLayout->nTracks); i++) {
buoy_track_t * cTrack = cLayout->tracks[i];
M3DVector3d * ctMain = cTrack->main_track;
double thickness = cTrack->thickness;
double rotation = getAngleD(cTrack->start[0], cTrack->start[2],
cTrack->end[0], cTrack->end[2]);
double full_disp = sqrt(pow((cTrack->end[0] - cTrack->start[0]), 2)
+ pow((cTrack->end[2] - cTrack->start[2]), 2));
// nBuoys is nBuoys per side. So one side has nBuoys/2 buoys.
for (j=0; j < cTrack->nMain; j+=2) {
double id = j*((full_disp)/(cTrack->nMain));
double y = (*(cTrack->f))(id);
double yp = (*(cTrack->fp))(id);
double normal, normal_a;
if (yp!=0) {
normal = -1.0/yp;
}
else {
normal = 999999999;
}
if (normal > 0) {
normal_a = atan(normal);
}
else {
normal_a = atan(normal) + PI;
}
ctMain[j][0] = id + ((thickness/2.0)*cos(normal_a));
ctMain[j][1] = 0;
ctMain[j][2] = y + ((thickness/2.0)*sin(normal_a));
ctMain[j+1][0] = id + ((thickness/2.0)*cos(normal_a+PI));
ctMain[j+1][1] = 0; // function pointers get set to null here
ctMain[j+1][2] = y + ((thickness/2.0)*sin(normal_a+PI));
}
for (j=0; j < cTrack->nMain; j++) {
rotatePointD(&(cTrack->main_track[j]), rotation);
}
}
}
Unless there are requirements for learning pointers or you cannot use STL, given you are using C++ I'd strongly recommend you use more STL, it is your friend. But anyways...
First, the type of ctMain is *M3DVector3D. So you can safely access ctMain[0], but you cannot access ctMain[1], maybe you meant for the type of ctMain to be **M3DVector3D, in which case the line for initialization you had written which is:
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
would make sense.
More Notes
Why are you allocating 30 of these here?
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
Given the type of main_track, you only need:
cTrack->main_track = (M3DVector3d *) malloc(sizeof(M3DVector3d));
In addition, for organizational purposes, when doing sizeof you may want to give the actual type to check the sizeof, as opposed to the variable (there should be no difference, just organizational), these two changes:
buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(buoy_layout_t*));
for (int i = 0; i < nTrackLayouts; i++) {
buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(buoy_layout_t));
}
cLayout->tracks = (buoy_track_t **) malloc(clayout->nTracks * sizeof(buoy_track_t*));
for (int i = 0; i < 1; i++) {
cLayout->tracks[i] = (buoy_track_t *) malloc(sizeof(buoy_track_t));
}