Best way of handling Constant Strings

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I am trying to have a Set of Constant string declared as pointers in Flash, I want then to copy the pointer to a struct, I am not sure what is the way to go with this.

I declared my string as static const char pointers:

static const char * GlobalTuningStr     = "Global Tuning";

static const char * KeyboardStr         = "Keyboard";

Also I have the following struct:

struct KeyboardStruct

{

  uint8_t ID;

  const char *name;

  uint8_t ActiveScaleMapping;

  uint8_t RootNote;

  uint8_t Octave;

 

  CYBIT OcatveMappingEnabled;

  uint8_t ScaleSize;

  uint8_t ScaleWidth;

  uint8_t ScaleStepsCounted;

  uint8_t Steps[12];

};

but when I call the MidiController_Keyboard_Start() function, USBUART prints trash.

void MidiController_Keyboard_Start()

{

    Keyboard -> name = KeyboardStr;

    SetActiveMapping( Keyboard, CHROMATIC);

   

    USBUART_PutString(Keyboard -> name);

   

  Keyboard->RootNote = 0;

  Keyboard->Octave = 0;

}

I am not sure how am I supposed to handle Strings in PSoC, any help?

Thanks!

0 Likes
1 Solution
NoriTan
Employee
Employee
25 sign-ins 5 questions asked 10 sign-ins

You add following line inside of the function MidiController_Keyboard_Start()

    struct KeyboardStruct *Keyboard = &KeyboardContent;

In this case, the LOCAL variable Keyboard is declared and the LOCAL variable is discarded when exiting from the function MidiController_Keyboard_Start() The pointer to the KeyboardContent will not be stored in the GLOBAL variable declared in the header file.

If you want to initialize the GLOBAL variable in the function, just assign the pointer without the type identifier.

    Keyboard = &KeyboardContent;

It is available to declare the struct without the pointer variable.  But I don't recommend this solution because the operator -> must be replaced with the operator . (dot) in all codes.

Regards,

Noriaki

View solution in original post

4 Replies
NoriTan
Employee
Employee
25 sign-ins 5 questions asked 10 sign-ins

The variable Keyboard is declared in the MidiController.h as follows.

GS003237.png

This variable declares a four byte length memory area of a "pointer to KeyboardStruct" as described in the MAP file as follows.

GS003238.png

Then where is location of the the structure?  What is the value of the pointer?

I executed "Find In Files" to find where the Keyboard content is assigned but I cannot.

GS003239.png

I recommend to declare the structure content like following.

    struct KeyboardStruct KeyboardContent;

    struct KeyboardStruct *Keyboard = &KeyboardContent;

Regards,

NTAN

0 Likes
Anonymous
Not applicable

Hi, thanks for your reply, I have a separate file with my structs, its a header file.

If I got right what you are saying to me, to use a pointer to a struct, I must declare the data as a struct and then declare a pointer that points to it? , I'm sorry for this "dumb" questions, I'm transitioning from Arduino and c++ to C (I think?), and I must re learn some of the basic stuff.

So thanks for the recommendation, I'm going to try that, thank you

So this is what I did is on my header file I declared the following:

    struct KeyboardStruct GlobalTuningContent;

    struct KeyboardStruct KeyboardContent;

    struct KeyboardStruct ChordGeneratorContent;

   

    struct KeyboardStruct *GlobalTuning;

    struct KeyboardStruct *Keyboard;

    struct KeyboardStruct *ChordGenerator;

and then on the appropriate Start function I pointed the pointer to the content, as done below:

void MidiController_Keyboard_Start()

{

    struct KeyboardStruct *Keyboard = &KeyboardContent;

   

    //start functions goes next

}

is this right? , I am still getting weird data in some places and want to make sure this is not the reason.

0 Likes
NoriTan
Employee
Employee
25 sign-ins 5 questions asked 10 sign-ins

You add following line inside of the function MidiController_Keyboard_Start()

    struct KeyboardStruct *Keyboard = &KeyboardContent;

In this case, the LOCAL variable Keyboard is declared and the LOCAL variable is discarded when exiting from the function MidiController_Keyboard_Start() The pointer to the KeyboardContent will not be stored in the GLOBAL variable declared in the header file.

If you want to initialize the GLOBAL variable in the function, just assign the pointer without the type identifier.

    Keyboard = &KeyboardContent;

It is available to declare the struct without the pointer variable.  But I don't recommend this solution because the operator -> must be replaced with the operator . (dot) in all codes.

Regards,

Noriaki

Anonymous
Not applicable

Thank you ! 

0 Likes