2

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.

3
  • 1
    You only have a maximum of 81 spaces. An array of N is indexed by 0..N-1. It seems to me you think that the array is sized to be N+1 because you say your 9x9 array holds 100 elements. It only holds 81. Commented Feb 18, 2017 at 2:45
  • 1
    Looks like you're looping from 0 to 9. Your array only supports 0 to 8. Commented Feb 18, 2017 at 2:45
  • Alright, after I changed the looping to go from 0-8 rather then 0-9 I still get a segmentation fault. Ideas ? Commented Feb 18, 2017 at 4:07

2 Answers 2

1
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';

Should be

(*mass_data)[mass_data_counter][mass_data_counter_two] = 'T';

Naturally, same goes for the line:

std::cout << *mass_data[mass_data_counter][mass_data_counter_two]

But really this pointer type is not necessary to pass array by reference in C/C++.

You should instead declare:

void mass_data_define(char mass_data[9][9]) {
    // To read:
    char z = mass_data[3][6];
    // To write:
    mass_data[2][1] = 'C';
}

// elsewhere
char my_mass_data[9][9];
mass_data_define(my_mass_data);
Sign up to request clarification or add additional context in comments.

4 Comments

What exactly is *mass_data[mass_data_counter][mass_data_counter_two] here? Is it some arbitrary address that it is pointing to?
Ah, yes I will change it to no longer use a pointer. However changing it from *mass_data[mass_data_counter][mass_data_counter_two] = 'T'; to (*mass_data)[mass_data_counter][mass_data_counter_two] = 'T'; still didn't fix the segmentation fault, when the pointer is kept.
Did you update both cases of using *mass_data[x][y] instead of the correct (*mass_data)[x][y]? I updated the answer.
Nope! Fixed now, Thanks!
0

Short Answer: Here's a one-line fix to your code so may continue to use a pointer and still not get a segmentation fault:

        (*mass_data)[mass_data_counter][mass_data_counter_two] = 'T';

Long Answer: Read Create a pointer to two-dimensional array.

For an in-depth understanding of how to pointers to access multi-dimensional arrays, read this.

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.