Problem with String Array at Library Functions

Hi, ::slight_smile:
I’m having a problem using a String Array while writing a library.
I’m writing a library to clean up my code and this library draws a Button Grid in a TFT and print their names based in a String Array.
So I want to define a String Array in my IDE like this:

String myGridValues[] = {"1","2","3","4","5","6","7","8","9","DEL","0","OK"};

And I want to use the Library Function like this, the String as a parameter:

myGrid.setupGrid(0,0,320,240,3,4,5,myGridValues);

But the Button Values showed at the TFT are strange. I searched it some time, and I think it’s something related to reference.
If I use this function directly coded at IDE it works all right.

Below is the Header file:

#ifndef TFT_Grid_h
#define TFT_Grid_h

#include "Arduino.h"
#include "UTFT.h"
#include "UTouch.h"
#include <string>

class TFT_Grid
{
	public:
		void setupGrid(int IntX, int IntY, int EndX, int EndY, int QuadX, int QuadY, int Grid, String GridValues[]);
	private:
		int _IntX, _IntY;
		int _EndX, _EndY;
		int _QuadX, _QuadY;
		int _Grid;
		String _GridValues[];
};
#endif

And here is the CPP file:

/*
IntX       >>   Starting X
IntY       >>   Starting Y
EndX       >>   Width of the area to fill
EndY       >>   Height of the area to fill
QuadX      >>   Number os columns
QuadY      >>   Number of rows
Grid       >>   Grid size
GridValues >> 	Buttons names and values
*/

#include "Arduino.h"
#include "TFT_Grid.h"

void TFT_Grid::setupGrid(int IntX, int IntY, int EndX, int EndY, int QuadX, int QuadY, int Grid, String GridValues[]){
	_IntX = IntX;
	_IntY = IntY;
	_EndX = EndX;
	_EndY = EndY;
	_QuadX = QuadX;
	_QuadY = QuadY;
	_Grid = Grid;
	
	for(int n=0;n<=_QuadX*_QuadY-1;n++){
	_GridValues[n] = GridValues[n];
	myGLCD.print(_GridValues[n],0,n*16);
	}
	myGLCD.print("DONE",0,224);
}

The _GridValues are very strange and different from myGridValues.

It’s not the whole code, but I think it’s enough to understand my problem. If needed I can post it all. Thanks for any help :smiley: .

Why are you using Strings at all? The strings look to be constant, so the overhead of the String class is a waste of resources.

	_GridValues[n] = GridValues[n];

Checking that n is a valid index is usually a good idea.

		String _GridValues[];

Just how many elements are in this array? What happens when you write beyond the end of this array (which you do)?

String _GridValues; needs a size, otherwise its just a pointer ( and not allowed by C++, its a GCC extension for C++ ).

Writing to it effectively writes the data to the end of the class instance in RAM. It has a use, but not here.

Here is a solution I answered for someone else which had the same problem. http://forum.arduino.cc/index.php?topic=281742.msg1977505#msg1977505

Thanks for your response.
I'm using Strings to make it variable, so I can change the String Array if I want to.
And about the number of elements, thinking that the Array is variable, the number of elements of _GridValues needs to be same of GridValue (that is QuadX*QuadY or a sizeof(GridValues) logic).

Declaring and not [12] (for example) doesn't mean the String Array will have the same Storage Size as it get filled with values?

If _GridValues have the same size of GridValues, I don't have to check the index, because I declared values at the IDE, Am I Right?

pYro_65:
String _GridValues; needs a size, otherwise its just a pointer ( and not allowed by C++, its a GCC extension for C++ ).

Writing to it effectively writes the data to the end of the class instance in RAM. It has a use, but not here.

Here is a solution I answered for someone else which had the same problem. http://forum.arduino.cc/index.php?topic=281742.msg1977505#msg1977505

As I am just a beginner at writing Library (C++ code), I will read the posts and try to understand the code structure. Thanks!

I could solve my problem.

In Header I changed from String _GridValues;[] to String *_GridValues;

And at the CPP I deleted _GridValues[n] = GridValues[n]; and added _GridValues = GridValues; before the print loop.

Below is the Header and CPP file for documentation:

#ifndef TFT_Grid_h
#define TFT_Grid_h

#include "Arduino.h"
#include "UTFT.h"
#include "UTouch.h"
#include <string>

class TFT_Grid
{
	public:
		void setupGrid(int IntX, int IntY, int EndX, int EndY, int QuadX, int QuadY, int Grid, String GridValues[]);
	private:
		int _IntX, _IntY;
		int _EndX, _EndY;
		int _QuadX, _QuadY;
		int _Grid;
		String *_GridValues;
};
#endif
/*
IntX       >>   Starting X
IntY       >>   Starting Y
EndX       >>   Width of the area to fill
EndY       >>   Height of the area to fill
QuadX      >>   Number os columns
QuadY      >>   Number of rows
Grid       >>   Grid size
GridValues >> 	Buttons names and values
*/

#include "Arduino.h"
#include "TFT_Grid.h"

void TFT_Grid::setupGrid(int IntX, int IntY, int EndX, int EndY, int QuadX, int QuadY, int Grid, String GridValues[]){
	_IntX = IntX;
	_IntY = IntY;
	_EndX = EndX;
	_EndY = EndY;
	_QuadX = QuadX;
	_QuadY = QuadY;
	_Grid = Grid;

        _GridValues = GridValues;
	
	for(int n=0;n<=_QuadX*_QuadY-1;n++){
	myGLCD.print(_GridValues[n],0,n*16);
	}
	myGLCD.print("DONE",0,224);
}

Now the solution appears to be very easy.
Thanks for everything!

Just use MASSIVE char arrays instead. It'll actually save you memory in the long run.