0

I have received an assignments and although not specified I would like to implement the use of pointer in it. My problem that I am encountering is that I believe I am not passing my 2D array correctly a second time. What I mean by second time is that I would like to pass the array from main to a function and then again from this function to a different function (if anyone knows of some appropriate terminology of doing this I would appreciate if you can share it.) Since I already wrote a bunch of other code, I will provide the method of how I am passing it, if I am doing it correctly I will provide the remaining of the code in order to find if there is another bug within the code.

int main(){
    char seats[rows][columns] //rows and columns are global integers
    assignSeats(&seats);
}
void assignSeats(char (*sPlan)[rows][columns]){
    assignFirstClass(sPlan);
}
void assignFirstClass(char (*sPlan)[rows][columns]){
    //user inputs for row and col
    (*sPlan)[row][col] = 'X';
}

Just to mention, I am not getting any error, just that I am not getting the value I am giving to the array position. Although the bug could be somewhere else I would first like to confirm that I am doing the passing of the array correctly since this is what is new to me. Thank you in advance for your help.

SOLUTION The problem was not with the method of passing the pointer, simply that instead of:

(*sPlan)[row][col] = 'X';

I wrote:

(*sPlan)[row][col];

It just went pass me multiple times.

  • 1
    Possible duplicate of [Passing a 2D array to a C++ function](http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – nitronoid Apr 22 '17 at 00:09
  • yeah I've seen it, and that's how I did the first pass. It does not however address the second pass. This is where my inquiry is. – Jorge Vargas Moreno Apr 22 '17 at 00:11
  • See this [question](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) for the different approaches to passing a 2D array – nitronoid Apr 22 '17 at 00:11
  • I did but I also read that the method I am using (*arrName)[rows][cols] is a better method and would like to learn in this way. – Jorge Vargas Moreno Apr 22 '17 at 00:14
  • The parameter is correct for the second pass. However your functions are lacking return types – M.M Apr 22 '17 at 00:28
  • sorry, all those functions are actually set to void except main which is set to int, I will edit this on my question – Jorge Vargas Moreno Apr 22 '17 at 00:34

1 Answers1

0

You can pass your array by reference by replacing your call with assignSeats(seats); and changing your function parameter to assignSeats( char (&sPlan)[rows][columns], do the same for your second function.

Your functions also need return types, and you are missing a bracket in your second function definition.

Suggested fix:

#include <iostream>

int main()
{
    char seats[rows][columns]; //rows and columns are global integers
    assignSeats(seats);
    std::cout<<seats[row][col];  //Check the function has worked
    return 0;
}

void assignSeats(char (&sPlan)[rows][columns])
{
    assignFirstClass(sPlan);
}

void assignFirstClass(char (&sPlan)[rows][columns])
{
    //user inputs for row and col
    sPlan[row][col] = 'X';
}
nitronoid
  • 1,049
  • 7
  • 20
  • Sadly this didn't do it either. – Jorge Vargas Moreno Apr 22 '17 at 00:33
  • @ Jorge what result are you expecting and how are you checking for it? – nitronoid Apr 22 '17 at 00:36
  • @ nitronoid after the function assignSeats(seats) in the main function I call another function to print the current values. Also before this assign I initialize all to '*'. When I print it before assignSeats(seats) I get all *. This is why I know the first passing is working properly. I am expecting an 'X' in a row and col selected by the user in the assignFirstClass function which I am not getting. – Jorge Vargas Moreno Apr 22 '17 at 00:39
  • @Jorge The current code prints an X for me, can you post the rest of your code? – nitronoid Apr 22 '17 at 00:42
  • @nitonoid I was about to copy all the code and then found the mistake I was doing. Since you gave me the most help I made your answer as the solution. Thank you for your help. – Jorge Vargas Moreno Apr 22 '17 at 00:51
  • Thanks! I'm glad you found the solution. – nitronoid Apr 22 '17 at 00:52