The objective of this program is of right now to set each array variable of the 2D array,
char mass_data_shift[9][9]; equal to 'T' . Which should equal 100 Ts overall.
This is done by calling a void function with this 2D array address as an argument. Calling it by a pointer to then be initialized in a loop.
Inside the loop is were the 2D array should be set to T.
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';
However..... the program results in:
- (Most Often) A Segmentation Fault after trying to initialize *mass_data[4][2]
- (Sometimes) A Segmentation Fault after the program successfully(?) runs.
- (Sometimes) The program successfully runs.
Meaning the program, somewhere, is writing out of bounds. Any help would be appreciated in both making the program run without a segmentation fault and/or fixing other mistakes.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
void mass_data_define(char (*mass_data)[9][9]){
int mass_data_counter;
int mass_data_counter_two = 0;
for (mass_data_counter=0;mass_data_counter<9;mass_data_counter++){
do {
std::cout << "|Array #1 " << mass_data_counter
<< " ::: |Array #2 " << mass_data_counter_two
<< std::endl;
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';
std::cout << *mass_data[mass_data_counter][mass_data_counter_two];
mass_data_counter_two++;
std::cout << "---------End of Counter 2 Init Code----------" << std::endl;
} while (mass_data_counter_two < 9);
mass_data_counter_two = 0;
}
}
int main()
{
char mass_data_shift[9][9];
mass_data_define(&mass_data_shift);
std::cout << "-END-" << std::endl;
return 0;
}
Final Edit: The main cause was solved by szym below. Sorry about the whitespaces and missing iostream , was a formatting issue when I made the post. Also changed the loop to fit the array length as suggested below.