简体   繁体   中英

adress pointer cast to struct

I got an issue with the usage of pointers with structs.

I have a databank and each entry is declared as struct (where the members represent the db-item).

Now I want to print each parsed entry into a file and have to use pointers to point to a specific entry in the database. The following code snippet curses the problem:

/* print database entries */
    i = 0;
    while (i < *(uint32_t *)(*ptr_to_s_20b_parse_entries + 0xc)) {
      ptr_s_db_entry = *(s_db_entry **)(ptr_to_s_20b_parse_entries[2] + i * 4);
      if (ptr_s_db_entry->show == 0) {
        printf("[%3d] [%s] [%s]\n",i,ptr_s_db_entry->ptr_e_title,ptr_s_db_entry->ptr_e_text);
      }
      i = i + 1;
    }
  }
  return;

The Error:

[Error] 's_db_entry' undeclared (first use in this function)

The following variables are declared:

  struct s_db_entry *ptr_s_db_entry;

struct s_db_entry {
    byte show; // 0 = show, 1 = don't show
    byte strlen_e_title;
    short strlen_e_text;
    int *ptr_e_title;
    int *ptr_e_text;
};

The ptr_to_s_20b_parse_entries is a parameter in from the caller.

Thank you already!

You'll need

(struct s_db_entry **)

instead of

(s_db_entry **)

since it's not typedef 'd.

Unless you did not show that part, s_db_entry is not a type in your code.

In the code

 ptr_s_db_entry = *(s_db_entry **)(ptr_to_s_20b_parse_entries[2] + i * 4);

need to be changed to

ptr_s_db_entry = *(struct s_db_entry **)(ptr_to_s_20b_parse_entries[2] + i * 4);
                   ^^^^^^

[Error] 's_db_entry' undeclared (first use in this function)

There aa couple of suggestions for this. Changing the definition to create a typedef of the struct will address the problem. Change this:

struct s_db_entry {
    byte show; // 0 = show, 1 = don't show
    byte strlen_e_title;
    short strlen_e_text;
    int *ptr_e_title;
    int *ptr_e_text;
};

To this: (also suggesting a name change here.)

//typedef struct s_db_entry {//name tag may be unnecessary at this point 
typedef struct {
    byte show; // 0 = show, 1 = don't show
    byte strlen_e_title;
    short strlen_e_text;
    int *ptr_e_title;
    int *ptr_e_text;
}db_entry_s;//note, typedef symbol is unique from name tag if both are to be used. 

This would of course require that

(struct s_db_entry **)

Become this:

(db_entry_s **)

(Note that it is more common to see a suffix _s to indicate a struct typedef than eg. prepending s_ .)

Also in your code you show this declaration struct s_db_entry ptr_s_db_entry; prior to defining the actual struct being referenced. It should be place beneath the struct (or struct typedef , should you choose to use that method.) definition.

typedef struct {
    byte show; // 0 = show, 1 = don't show
    byte strlen_e_title;
    short strlen_e_text;
    int *ptr_e_title;
    int *ptr_e_text;
}db_entry_s;

db_entry_s *ptr_db_entry_s;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM