strange typedef behavoir

Hallo and an happy new year to all,

I’ve a small program for my ARDUINO UNO where I want to address scan codes and have problems with the initialization of the code:

// Lookup Table
typedef struct {
  const char *token;
  int   seq;
} RScanCode;

// Init SCODES
RScanCode sCodes[] = {
  { "A",  0x1E },
  { "B",  0x30 },
  { "<F1>", 0x3b },
  { "<F2>", 0x3c },
  { "<DEL>", 0x53 },
  { "<SPC>", 0x29 } 
};

The compilation process gives me these errors.

test_ether_stuff.cpp:11:43: error: ‘RScanCode’ does not name a type
test_ether_stuff.cpp:11:54: error: ISO C++ forbids declaration of ‘codes’ with no type [-fpermissive]
test_ether_stuff.cpp:12:47: error: ‘RScanCode’ has not been declared

Makefile:
CC = gcc                                                 
CFLAGS  = -Wall -g  -std=c99                             
LDFLAGS = -lm                                            
                                                         
OBJ = pstring.o                                          
                                                         
all: $(OBJ)                                              
        $(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)        
                                                         
%.o: %.c                                                 
        $(CC) $(CFLAGS) -c $<

I’ve a attached a working draft, that workings with gcc and linux. What I’m doing wrong. THX in advance huck

pstring.c (1.13 KB)

// Lookup Table
typedef struct {
  const char *token;
  int   seq;
} RScanCode;

// Init SCODES
RScanCode sCodes[] = {
  { "A",  0x1E },
  { "B",  0x30 },
  { "<F1>", 0x3b },
  { "<F2>", 0x3c },
  { "<DEL>", 0x53 },
  { "<SPC>", 0x29 } 
};

void setup()
{
  Serial.begin(115200);
  Serial.println(sCodes[0].token);
}

void loop()
{
}

compiles just fine for me.

Since you didn’t say what version of the IDE or which Arduino, I won’t either.

PaulS:
compiles just fine for me.

Since you didn’t say what version of the IDE or which Arduino, I won’t either.

He actually said for his Arduino UNO very early in the post!

You are so funny. Yes it does up to the point you took it… But try passing a parameter into a function using the struct you defined… such as what came next…

eg

// Lookup Table
typedef struct {
  const char *token;
  int   seq;
} RScanCode;

// Init SCODES
RScanCode sCodes[] = {
  { "A",  0x1E },
  { "B",  0x30 },
  { "<F1>", 0x3b },
  { "<F2>", 0x3c },
  { "<DEL>", 0x53 },
  { "<SPC>", 0x29 } 
};

void indexOfToken(char *token, RScanCode *codes, int numCodes) {
// clever stuff here
}

void setup()
{
  Serial.begin(115200);
  Serial.println(sCodes[0].token);
}

void loop()
{
}

That will fail, and the IDE error message is not helpful… So if you feel like being helpful, go for it…

Happy New Year all,

Regards,

Graham

Edit: For completeness 1.0.5 1.5.7 1.5.8 1.6.0 - Makes no difference to the error, and also, DUE and MEGA2560 exactly the same…

Thx, hm for me not, may besomething else is wrong here the complete script:

#include <stdio.h>
#include <string.h>

typedef struct {  const char *token;  int   seq; } RScanCode;

RScanCode sCodes[] = {
  { "A",  0x1E },
  { "B",  0x30 },
  { "<F1>", 0x3b },
  { "<F2>", 0x3c },
  { "<DEL>", 0x53 },
  { "<SPC>", 0x29 } 
};

int indexOfToken(const char *token, const RScanCode *codes,const int numCodes) {
    int  ix = 0;
    char *tkFound = NULL;
    while (ix < numCodes && !tkFound ) {
      if ( strcmp(token, codes[ix].token) == 0) {
        return ix;
      }
      ix++;
    }
    return -1;
}

void sendKeys(char *tkSeq, const char *tkSep, RScanCode *codes, int numCodes) {                                                                                                                              
  char *tkCur = NULL; 
  tkCur = strtok (tkSeq, tkSep);
  while ( tkCur != NULL) {
    int  index  = indexOfToken(tkCur, codes, numCodes); 
    if (index >= 0) {                                                                                                               
       Serial.print(tkCur);
       Serial.print(": ");
       Serial.println(sCodes[index].seq);
     } else {
       Serial.print(tkCur);
       Serial.println(": not found!");
    }               
    tkCur = strtok (NULL, tkSep);
  }               
}     

void setup() {
    
     Serial.begin(9600);

    char data[]  = "A B C <F1> <F2> <DEL> <SPC>";          
    const char *separator = " ";                           
    const int numCodes = sizeof(sCodes)/sizeof(RScanCode); 
    sendKeys(data, separator, sCodes, numCodes);                   
          
}

void loop () {}

and the errors

avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections \
-fdata-sections -mmcu=atmega328p -DF_CPU=16000000L \
-MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=101 \
-I/usr/share/arduino/hardware/arduino/cores/arduino \
-I/usr/share/arduino/hardware/arduino/variants/standard /tmp/build4097359223163668069.tmp\test_typedef.cpp \
-o /tmp/build4097359223163668069.tmp/test_typedef.cpp.o\

test_typedef.cpp:5:43: error: ‘RScanCode’ does not name a type
test_typedef.cpp:5:54: error: ISO C++ forbids declaration of ‘codes’ with no type [-fpermissive]
test_typedef.cpp:6:47: error: ‘RScanCode’ has not been declared

huckfinn: Thx, hm for me not, may besomething else is wrong here the complete script:

Don't worry about it, it is not you, he just enjoys posts like the one you just saw, but I will be super impressed if he comes up with an actual valid solution to your problem having pointed out the error of his reply!! ;)

Happy new year.

Graham

here the complete script:

Ah, that's better :)

Create a new tab called (for example) myTypes.h - this will create a header file of that name in the same folder as your program.

Delete the typedef from your program and put it instead into that tab.

// Lookup Table
typedef struct {
  const char *token;
  int   seq;
} RScanCode;

In your program, replace the above with ...

#include "myTypes.h"

This should get round a peculiarity of the Arduino IDE.

if you stick a prototype for indexOfToken somewhere after your typedef and before your func def your issue should be gone.

It's the flawed nature of the IDE's prototype generation. This is compounded by it using the error line number returned by gcc from the .cpp to highlight the code in the sketch - of course these don't match so you get a highlight on completely the wrong line in the IDE.

if you take a look at test_typedef.cpp in your build folder (identified in your error message) all will become clear.

Thank you, this will do the job. :grin: