Trouble with static 2D-Array in function

Problem description: I'm currently working on a program that contains a 2D-Array in a function. Since I wish to update it inside another function I declared it a static array like this:

1
2
3
4
5
 static char arr[A_M][A_N] = { '.', '.', '.' ....... }; 

 chFunction(arr, SIZE);

...


The problem is that once I try to pass this array to another function, where the dots are replaced by actual characters A ... D ... R so on so forth, the following problem occurs.


When I run it thru the debugger, having a breakpoint on the code that changes the dots to A for instance in the other function, it says that symbols can not be found when I hover over the array variable ... This ONLY happens when I declare the array static, when I set it to simply char arr[][] ... and I hover over the variable in the other function, no error of missing symbols.

It also happens if I simply want to display the content of the array in a loop. Once breakpoint is set in the other function, and hovering over it, it gives me this missing symbols from symbol file ...

I'm still very much beginner, but as I understand it, the symbol that cannot be loaded/found in the other function, it must have to do with the array variable that is passed via function call to the other function.

If i put the same code that changes the array into the function (where I don't wish to have it ...), and even though set to static, and I hover over the changing function, it behaves like normal, no symbol missing message appearing while hovering over it. This hardens the evidence that it must have to do with passing the array to the other function like so:

 
 changeArr(arr, SIZE);


And at that point I'm totally lost. How would I declare a static array, and pass it to the other function, so that it keeps its data inside one function, once updated in another? Or, rather the question is, how would I pass a static array to another function, so that the symbol while debugging still is loaded, or processed, once passed to it?

I'm working with the community edition of VS 2015. And this is the only and first time I ran into this problem. Probably because it is also the first time I use an array set to static, which I would need, to update it from one function to the next. So, if you know a solution to this, please let me know.

Edit: The program still works as should be, even with the symbol file missing during debugging. Yet, still, this is an error of sorts, that I feel should not be there and I wish to get rid of it.
Last edited on
Sorry for my poor English, but... when you say “static”, do you mean “const”? Do you want your array to be modified by different functions or not? I didn’t understood.

When you pass an array as an argument of a function, you are passing a pointer. That means you can’t prevent it to be modified, unless you declare it “const”.

On the other hand, to let it being modified, it doesn’t need to be “static”.

I know you’ve already tried to explain your problem the best, but could you please clarify better what’s your purpose?
First of, thanks for your answer! To answer your first question, by static i mean static (the keyword).

My purpose is this:

Having one function that only displays and updates the array, another function that contains the update code.

Here is a small working code for the two functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void displayArray()
{
     // GCONST_X / Y are global constants both [3][3] 
     static char arr[GCONST_X][GCONST_Y] = { { '.', '.', '.' }, { '.', '.', '.' }, { '.', '.', '.' } };

     for (int x = 0; x < GCONST_X; ++x)
     {
        for (int y = 0; y < GCONST_Y; ++y)
        {
             cout << setw(10) << right << arr[x][y];
        }
     }

     changeArr(arr, SIZE);
}

void changeArr(char [][INDEX_Y], SIZE)
{
   int pos_x = 0, pos_y = 0;
   int count = 0;   

   cout << "Enter x: ";
   cin >> pos_x;

   cout << "Enter y: ";
   cin >> pos_y;
  
   count += 1;

	if (count % 2 == 1)
	{
		arr[pos_x][pos_y] = 'A';
	}
	else
	{
		arr[pos_x][pos_y] = 'B';
	}
}


Back in the first function, then, the array gets the information and reflects this change if set to static. If not set to static, no change takes place, the '.' at the position remain .'s (dots).

That is the gist of things so far as the code is concerned. Now to my problem in other words. Doing it like this, ends up with breakpoint, and by hovering over arr[x][y], is telling me that a symbol is missing from symbol file. But ONLY if the array in function one is declared static.

-

You pointed out that an array should be updated, because only a pointer to an array is passed, but it doesn't do even that, which is mysterious. Declared and initialized like so:

 
       char arr[GCONST_X][GCONST_Y] = { { '.', '.', '.' }, { '.', '.', '.' }, { '.', '.', '.' } };


I hope I made my problem a bit clearer now. And, again, thanks for your reply. :)


EDIT: This is the exact message that I receive when doing it like this:

+ arr Project1.exe!0x0130c000 (Type information missing from symbol file) {46 '.', 46 '.', 46 '.'} char[3] *
Last edited on
You need to supply parameter names in your function:

void changeArr(char [][INDEX_Y], SIZE)

should be

void changeArr(char [][INDEX_Y] arrayname, SIZE n).
I did, sorry, my example code was a bit messed - edited it. But it does not change the underlying problem. And I know it MUST have to do with my code, and certainly is nothing related to VS ...
If you declare "arr" inside "displayArray()", it is destroyed as soon as you exit the function.
That's why you needed to declare it static - a static variable 'survives' after the destruction of the function.
There are alternatives: you could declare it global (outside of every function) or you could declare it in main() and then pass it to functions.
Here an example based on your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iomanip>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::right;
using std::setw;

constexpr int GCONST_X = 3;
constexpr int GCONST_Y = 3;

void displayArray(char arr[GCONST_X][GCONST_Y]);
void changeArr(char arr[GCONST_X][GCONST_Y]);

int main()
{
    char arr[GCONST_X][GCONST_Y] = { { '.', '.', '.' }, { '.', '.', '.' }, { '.', '.', '.' } };
    displayArray(arr);
    changeArr(arr);
    displayArray(arr);

    cout << endl;
    return 0;
}

void displayArray(char arr[GCONST_X][GCONST_Y])
{
     for (int x = 0; x < GCONST_X; ++x)
     {
        for (int y = 0; y < GCONST_Y; ++y)
        {
             cout << setw(10) << right << arr[x][y];
        }
     }
}

void changeArr(char arr[GCONST_X][GCONST_Y])
{
   int pos_x = 0, pos_y = 0;
   int count = 0;

   cout << "\nEnter x: ";
   cin >> pos_x;

   cout << "Enter y: ";
   cin >> pos_y;

   count += 1;

   if (count % 2 == 1)
   {
      arr[pos_x][pos_y] = 'A';
   }
   else
   {
      arr[pos_x][pos_y] = 'B';
   }
}
Thank you very much for going through all the trouble!

Yes, this works, and the problem is gone. That everything in a function once is exited is destroyed is exactly the reason why I tried the approach with

static char arr[][]

so the array would remain initialized, and updated, after each function call. (On a side-note, I also read that it doesn't seem to be certain that a static array indeed remains initialized between function calls ...) Which led to the problem in the first place. I specifically wanted it to be that way, and not clutter the main function with any code when it doesn't have to be. There are many more functions that would have accessed the array containing '.'s, all in one way or other altering this array. And the most convenient way would have been the way I tried doing it, which again led to the problem.

The other way I tried was placing the code in totality into this one function. Though it made little sense and introduced redundancy which I felt was not needed. One function, one purpose.

Also because you mention it. I thought that it is bad practice to place anything above main(), so whatever it is can be accessed globally? (Or even dangerous?) Or was it only if they are not const variables? Or constexpr? Something along the lines it was.

Well, once again thank you for your effort to help me! Leaves me the task to find a different approach to write my program. :-))
Last edited on
You welcome, I did very little ;-)
I appreciate what you say and I think it's correct, but if your array is to be used by a lot of functions, I don't think it will be easy to declare it inside one of them, if that's not main().
For example, once you declared the array inside displayArray(), you need to call changeArr() from there. At that point, if you want to display it again, you'll have to call displayArray() again, which would call changeArr() automatically, and so on... Your code would become really complicated in just a few steps, I'm afraid.

I agree with your concerns about things that are declared "above main()", but don't you think those are the problems to solve which classes come in handy?
Good luck with your coding!
Don't underestimate how much you helped me, not only with code!

As far as complexity goes, no, quite the opposite is the case. In my original code I have 5 functions, 1 call to displayArray in main, 1 to update, and from update the array was passed down to two other functions doing input validation before the array is populated with either A or B. One of the five functions contains a loop that keeps the cycle alive while a certain condition is not met.

This approach, hadn't there been this failure with the symbols that truly only happens when the array in a function is set to static, has taken all of 3 function calls via a loop. The main reason for trying to do it like this is that I wanted to simply call the display function from a menu, and didn't want to introduce variables, or the array to be passed down to the other functions into THAT function header. Sorry, this might sound stupid, but I felt that this would only introduce lots of redundancy to the code. Meaning, why introducing something to a function/function header, that isn't needed - where a void function doing its thing just fine would do. (I hope I make sense here.)

And as to classes, agreed. They would be helpful. Sadly that is something I would have yet to learn and know nothing about. Way over my head so to say. The long and short of it is - classes are out of the question.

Thanks very much once more, and, yes, luck is really what I need right now! :)
Topic archived. No new replies allowed.