Pointer to array of pointers points to unexpected address - c

The question is related to my former question here:
Arduino compile error while using reference to a struct in another struct
I ported the sample code back to the pc and compiled it to get a clue, what's going wrong.
Here's the sample:
#include <stdio.h>
unsigned int steps=64;
unsigned int mode=0;
unsigned int speed=1;
typedef struct{
unsigned int option_value;
char option_name[17];
} SELECTION;
typedef struct{
char item_name[17];
unsigned int* variable;
SELECTION** options;
} MENU_ITEM;
SELECTION mode_forward = { 0, "Forward" };
SELECTION mode_backward = { 1, "Backward" };
SELECTION* options_mode[] = { &mode_forward, &mode_backward };
SELECTION speed_slow = { 0, "Slow" };
SELECTION speed_normal = { 1, "Normal" };
SELECTION speed_fast = { 2, "Fast" };
SELECTION* options_speed[] = { &speed_slow, &speed_normal, &speed_fast };
MENU_ITEM menu_steps = { "Steps", &steps, NULL };
MENU_ITEM menu_mode = { "Mode", &mode, options_mode };
MENU_ITEM menu_speed = { "Speed", &speed, options_speed };
MENU_ITEM menu_exit = { "Exit", NULL, NULL };
const unsigned char menu_items = 4;
MENU_ITEM* menu_list[menu_items] = { &menu_steps, &menu_mode, &menu_speed, &menu_exit };
//-----------------------------------------------------------
int main(){
int options;
options=(int)(sizeof(options_speed)/sizeof(options_speed[0]));
printf("Speed options: %i\n\n",options);
printf("Address of speed_slow: %p\n",&speed_slow);
printf("Address of speed_normal: %p\n",&speed_normal);
printf("Address of speed_fast: %p\n",&speed_fast);
printf("Address of array: %p\n\n",&options_speed);
MENU_ITEM item;
item=*menu_list[2];
printf("Menu Item: %s - Item Value: %i\n",item.item_name,*item.variable);
printf("Address of name: %p\n",&item.item_name);
printf("Address of variable-pointer: %p\n",&item.variable);
printf("Address of options-pointer: %p\n",&item.options);
printf("Value of options-pointer: %p\n",*item.options);
return 0;
}
When i start the program i get the following output:
Speed options: 3
Address of speed_slow: 0x6010c0
Address of speed_normal: 0x6010e0
Address of speed_fast: 0x601100
Address of array: 0x601120
Menu Item: Speed - Item Value: 1
Address of name: 0x7fff18a5dc90
Address of variable-pointer: 0x7fff18a5dca8
Address of options-pointer: 0x7fff18a5dcb0
Value of options-pointer: 0x6010c0
That's what i expect....except the last line. Shouldn't the address that it points to be 0x601120 - the address of options_speed array?
Why does it point to the first member of the array instead?
What do i have to change to let it point to 0x601120?

You are evaluating *item.options, not item.options. This seems is not what you wanted to print (i.e. the "options pointer") as there is an extra dereferencing operation.

Related

Array of structure pointer in C

I am running following code.
#include <stdio.h>
struct point
{
int formatbyte1;
int formatbyte2;
int formatbyte3;
};
const struct point SW_VERSN_POINT =
{
1,2,3
};
const struct point SWMVERSN_POINT =
{
4,5,6
};
const struct point *object_directory[] =
{
&SW_VERSN_POINT,
&SWMVERSN_POINT,
0
};
int main()
{
printf("%ld\n", sizeof(SW_VERSN_POINT.formatbyte1));
printf("%ld 0X%X, 0x%X\n", sizeof(struct point), &SWMVERSN_POINT.formatbyte1, &SW_VERSN_POINT.formatbyte1);
printf("%ld 0X%X, 0x%X\n", sizeof(object_directory), object_directory[0], object_directory[1]);
return 0;
}
The output of above program is
4
12 0XE2BF0788, 0xE2BF0778
24 0XE2BF0778, 0xE2BF0788
But when I modified the object_directory, removed 0 from it
const struct point *object_directory[] =
{
&SW_VERSN_POINT,
&SWMVERSN_POINT
};
Now output of above program is
4
12 0XF967D788, 0xF967D778
16 0XF967D778, 0xF967D788
Why is the size of object_directory is changes from 24 to 16. (How does 16 come from)
Thanks in advance.
This code is running on Linux.

Array of jump tables in C

I'm trying to optimize access to some jump tables I have made, they are as follows:
int (*const usart_ctrl_table[USART_READ_WRITE_CLEAR])() =
{zg_usartCtrlRead, zg_usartCtrlWrite, zg_usartCtrlClr};
int (*const usart_frame_table[USART_READ_WRITE_CLEAR])() =
{zg_usartFrameRead, zg_usartFrameWrite, zg_usartFrameClr};
int (*const usart_trig_ctrl_table[USART_READ_WRITE_CLEAR])() =
{zg_usartTrigctrlRead, zg_usartTrigctrlWrite, zg_usartTrigctrlClr};
As you can see, the functions are for accessing a usart peripheral on a hardware level and are arranged in the table in the order of read/write/clear.
What I am attempting to do is have another jump table of jump tables, this way I can either run through initializing all the usart's registers in startup or simply change a single register later if desired.
i.e.
<datatype> (*usart_peripheral_table[<number of jump tables>])() =
{usart_ctrl_table, usart_frame_table, usart_trig_ctrl_table};
This way I can expose that table to my middleware layer, which will help maintain a standard across changing HALs, and also I can use a define to index this table i.e.
fn_ptr = usart_peripheral_table[CTRL_TABLE]
fn_ptr[WRITE](bitmask);
fn_ptr[READ](buffer);
As you may have already guessed, I am struggling to figure out how to construct this table. I figured it is one of two things:
Another simple array of pointers, as even a jump table itself is just an array of pointers. Hence my initialization would be:
const int* (*usart_peripheral_table[<number of jump tables])() =
{usart_ctrl_table, usart_frame_table, usart_trig_ctrl_table};
However this doesn't seem to be working. Then I thought:
An array of pointers to pointers. So I tried all kinds of combos:
const int**(*usart_perip...
const int**(usart_perip...
const int** (*usart_peripheral_table[<number of jump tables])() =
{&usart_ctrl_table, &usart_frame_table[0], usart_trig_ctrl_table};
Nothing seems to work. Do I need to store the address of the lower jump tables in yet another pointer before assigning that variable to a pointer-to-pointer array? i.e.
int* fn_ptr = usart_ctrl_table;
<dataytype>(*const usart_periph[<number>])() = {fn_ptr};
Thanks in advance, any help would be greatly appreciated.
MM25
EDIT:
const int** (*const peripheral_table[1])() =
{&usart_ctrl_table[0]};
const int** (*const peripheral_table[1])() =
{usart_ctrl_table};
The above both give the error "initialization from incomaptible pointer type", as do all other combinations I have tried
You might find that defining a typedef for your function pointers makes your code easier to read and maintain (although I’ve seen people recommend against it too):
#include <stdio.h>
#include <stdlib.h>
#define UART_RWC 3U
typedef int (*uart_ctl_func)(void);
int uart_read(void)
{
printf("Read.\n");
fflush(stdout);
return 0;
}
int uart_write(void)
{
printf("Write.\n");
fflush(stdout);
return(0);
}
int uart_clear(void)
{
printf("Clear.\n");
fflush(stdout);
return 0;
}
uart_ctl_func uart_ctl_jump_table[][UART_RWC] = {
{ uart_read, uart_write, uart_clear },
{ uart_read, uart_write, uart_clear }
};
int main(void)
{
uart_ctl_jump_table[0][1](); // Write.
uart_ctl_jump_table[1][0](); // Read.
uart_ctl_jump_table[1][2](); // Clear.
return EXIT_SUCCESS;
}
The next step might be to make the jump table a struct so you end up writing Uart_ctl_table.frame.read(), or to at least define an enum for the constants.
#include <stdio.h>
#include <stdlib.h>
#define UART_RWC 3U
typedef int (*uart_ctl_func)(void);
int uart_read(void)
{
printf("Read.\n");
fflush(stdout);
return 0;
}
int uart_write(void)
{
printf("Write.\n");
fflush(stdout);
return(0);
}
int uart_clear(void)
{
printf("Clear.\n");
fflush(stdout);
return 0;
}
typedef struct {
uart_ctl_func read;
uart_ctl_func write;
uart_ctl_func clear;
} uart_ctl_set_t;
typedef struct {
uart_ctl_set_t ctrl;
uart_ctl_set_t frame;
uart_ctl_set_t trig;
} uart_ctl_table_t;
const uart_ctl_table_t uart_ctl_table = {
.ctrl = { uart_read, uart_write, uart_clear },
.frame = { uart_read, uart_write, uart_clear },
.trig = { uart_read, uart_write, uart_clear }
};
int main(void)
{
uart_ctl_table.ctrl.write(); // Write.
uart_ctl_table.frame.read(); // Read.
uart_ctl_table.trig.clear(); // Clear.
return EXIT_SUCCESS;
}
Just add a * like you added [] when defining an array.
int zg_usartCtrlRead();
int zg_usartCtrlWrite();
int zg_usartCtrlClr();
int zg_usartFrameRead();
int zg_usartFrameWrite();
int zg_usartFrameClr();
int zg_usartTrigctrlRead();
int zg_usartTrigctrlWrite();
int zg_usartTrigctrlClr();
int (*const usart_ctrl_table[])() =
{zg_usartCtrlRead, zg_usartCtrlWrite, zg_usartCtrlClr};
int (*const usart_frame_table[])() =
{zg_usartFrameRead, zg_usartFrameWrite, zg_usartFrameClr};
int (*const usart_trig_ctrl_table[])() =
{zg_usartTrigctrlRead, zg_usartTrigctrlWrite, zg_usartTrigctrlClr};
int (* const * const usart_peripheral_table[])() =
{usart_ctrl_table, usart_frame_table, usart_trig_ctrl_table};
Usage:
usart_peripheral_table[1][2](5, 1, 3, 5, 6);
Btw, an empty parameter list on function declaration () means unspecified number and type of arguments. Do (void) if you want no arguments passed to your function.
This:
const int* (*usart_peripheral_table[<number of jump tables])();
Is an array of functions pointers that take unspecified number of arguments and return a pointer to constant integer.
This:
const int** (*usart_peripheral_table[<number of jump tables])()
Is an array of function pointers that take unspecified number of arguments and return a pointer to a pointer to a constant integer.
You can also go with a 2D array:
int (* const usart_peripheral_table_2d[][3])() = {
{
zg_usartCtrlRead, zg_usartCtrlWrite, zg_usartCtrlClr,
}, {
zg_usartFrameRead, zg_usartFrameWrite, zg_usartFrameClr,
}, {
zg_usartTrigctrlRead, zg_usartTrigctrlWrite, zg_usartTrigctrlClr,
},
};
But maybe you want to write accessor functions that will return a pointer to an array of functions. Nothing simpler!
#include <stddef.h>
int (*usart_ctrl_table_get(size_t idx))() {
return usart_ctrl_table[idx];
}
int (*usart_frame_table_get(size_t idx))() {
return usart_frame_table[idx];
}
int (*usart_trig_ctrl_table_get(size_t idx))() {
return usart_trig_ctrl_table[idx];
}
int (* const (* const usart_peripheral_table_indirect[])(size_t))() = {
usart_ctrl_table_get,
usart_frame_table_get,
usart_trig_ctrl_table_get,
};
Usage sample:
int main() {
usart_peripheral_table_indirect[2](1)();
}

Initializing a struct in C

Im having trouble initialising structures (well doing everything actually, but structures first). The struct is first made in a header as follows
typedef enum cell
{
BLANK, RED, CYAN
} Cell;
#define NAMELEN 20
typedef struct player
{
char name[NAMELEN + NULL_SPACE];
Cell token;
unsigned score;
} Player;
void initFirstPlayer(Player * player);
void initSecondPlayer(Player * player, Cell token);
#endif
=======================================================================
and I tried to initialise it here
void initFirstPlayer(Player * player)
{
int randNo = rand() % 2;
if (randNo == 0) {
token = RED;
}
else() {
token = CYAN;
}
player ; p1 = {
"placeholder",
token,
0,
}
}
void initSecondPlayer(Player * player, Cell token)
{ }
What is the correct way to initialise this player struct?
I suspect this should work for you. Use a generic initPlayer function. Use that to allocate memory for the player and set the initial values. Be sure to also include a freePlayer function where you free() the player when you're done.
#include <stdlib.h>
#include <string.h>
Player* initPlayer()
{
Player* player = malloc(sizeof(Player));
int randNo = rand() % 2;
if (randNo == 0) {
player->token = RED;
}
else {
player->token = CYAN;
}
const char* initName = "placeholder";
strcpy(player->name, initName);
player->score = 0;
return player;
}
void freePlayer(Player* p)
{
free(p);
}
The way you'd use this would be like so:
int main()
{
Player* p1 = initPlayer();
Player* p2 = initPlayer();
play(p1, p2);
freePlayer(p1);
freePlayer(p2);
}
Assuming you have at least C99 support, so that compound literals and designated initializers are available to you, then you can use:
void initFirstPlayer(Player *player)
{
*player = (Player){ .token = rand() % 2 ? CYAN : RED,
.score = 0,
.name = "placeholder"
};
}
This does a structure assignment to the variable whose address is passed to the function. It compresses it all into one statement; you can split it out into several if you wish. This is an occasion where the ternary ? : operator is useful. You might prefer (rand() % 2) with the extra parentheses; I'd probably add them as often as I'd omit them.
The compound literal comes from (typename){ ...initializer for typename... }.
The designated initializers are the .member = value notations.
If you're stuck with C90 support, you have to work harder, perhaps creating a local variable with the correct information and then doing the structure assignment.
void initFirstPlayer(Player *player)
{
Player p1 = { "placeholder", rand() % 2 ? CYAN : RED, 0 };
*player = p1;
}
Now the onus is on you to list the initializers in the correct sequence.
Another way is to receive the player you want to inicialize as parameter:
void initPlayer(Player* player)
{
int randNo = rand() % 2;
if (randNo == 0) {
player->token = RED;
}
else {
player->token = CYAN;
}
const char* initName = "placeholder";
strcpy(player->name, initName);
player->score = 0;
}
int main() {
Player p1;
initPlayer(&p1);
}
You can have an array of players or allocate dinamically with malloc.

How to print out the members of a struct, weird errors?

I've been trying to print of the members of a struct I have created, however there are a few declarations errors that are showing saying my structs are undeclared. I have a separate function for printing the members of the struct. I have no idea on how to debug it... please help
I have errors such as game1- undeclared (first use in this function) and expected = , ; asm or attribute before { token
#include <stdio.h>
#include <stdlib.h>
struct video_game
{
char *name, *genre, *developer, *platformer, *app_purchase;
int release_year, age_limit;
float price;
};
void print_video_game_details(struct video_game* s)
{
printf("\nTitle: %s\n", s->name);
printf("Genre: %s\n", s->genre);
printf("Developer: %s\n", s->developer);
printf("Year of Release: %d\n", s->release_year);
printf("Lower Age Limit: %d\n", s->age_limit);
printf("Price: $%f\n", s->price);
printf("In-app Purchase: %s\n", s->app_purchase);
}
int main(int agrc, char* agrv[])
{
struct video_game game1
{
game1.name = "Candy Crush Saga";
game1.genre = "Match-Three Puzzle";
game1.developer = "King";
game1.release_year = 2012;
game1.platform = "Android, iOS, Windows Phone";
game1.age_limit = 7;
game1.price = 0.00;
game1.app_purchase = "Yes";
};
struct video_game game2
{
game2.name = "Halo 4";
game2.genre = "First Person Shooter";
game2.developer = "343 Industries";
game2.release_year = 2014;
game2.platform = "Xbox 360, Xbox One";
game2.age_limit = 16;
game2.price = 69.95;
game2.app_purchase = "No";
};
struct video_game game1
{
game3.name = "Uncharted 2: Among Thieves";
game3.genre = "Action adventure RPG";
game3.developer = "Naughty Dog";
game3.release_year = 2012;
game3.platform = "PS3";
game3.age_limit = 16;
game3.price = 30.00;
game3.app_purchase = "No";
};
print_video_game_details(&game1);
print_video_game_details(&game2);
print_video_game_details(&game3);
return 0;
}
Your instance creations (game1, game2 and game3) are not C, they are using some made-up syntax.
They should be something like
struct video_game game1 = {
.name = "Candy Crush Saga",
/* ... */
};
You need to define three variables of type struct video_game, and <type> <name> [= <initializer>] is (roughly) how variables are defined in C.
If you don't have C99, it must be:
struct video_game game1 = {
"Candy Crush Saga",
"Match-Three Puzzle",
"King",
"Android, iOS, Windows Phone",
"Yes",
2012,
7,
0.00
};
Things to notice, that you seem to be ignoring:
No names of fields inside the initializer, just values.
The order must be exactly the same as when the struct was declared; first five strings, then two integers, then a float.
Values are separated with commas, not semicolons.

Problems Initializing Structures

Here are (some of) the structures that I am using; they are in a .h file:
struct rss_s {
Radio_types device_type; // Its device_type which is defined by the typedef above Radio_Types
char * device_info; // some thing about the radio NAV/COM/etc.
char * device_model; // the Manufactures part/model number.
char * device_serial; // the device's serial number..
int power_48v; // power to the unit..
int power_400hz;
int panel_lamps; // turn off or on the Panel Lamps only
void * radio_info;
};
typedef struct tuner_s { // when we talk about 'sub-radios' we are really saying how many tuners are there??
char * device_name; // OS-name
int frequency[tuned];
int power;
int dial_lamp;
int fd[ ]; // file descriptors
}tuner;
//// 614L8 ::= C614L8
typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;
struct radio_s_614L8 {
loopsw_614L8 loop_sw_614L8;
modesw_614L8 mode_sw_614l8;
int sw_band;
int sw_bfo;
int meter;
tuner * Tuner;
int tuners;
};
Now file main.c, which has all of the normal includes:
// Radio 614L8<br>
static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
static tuner tuner_614L8 = { { .device_name = "/dev/TBD", } };
static struct rss_s radios[] = {
{ C614L8, "ADF", "614L8", "8384", & radio_C_614L8,},};
// now comes the normal main()
The errors that I have:
error: field name not in record or union initializer
error: (near initialization for ‘radio_614L8.loop_sw_614L8’)
error: ‘tuner_614L8’ undeclared here (not in a function)
error: field name not in record or union initializer
error: (near initialization for ‘radio_614L8.loop_sw_614L8’)
error: ‘tuner_C_614L8’ undeclared here (not in a function)
error: field name not in record or union initializer
error: (near initialization for‘tuner_614L8.device_name’)
error: ‘radio_C_614L8’ undeclared here (not in a function)
You currently have:
static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
static tuner tuner_614L8 = { { .device_name = "/dev/TBD", } };
You need:
static tuner tuner_614L8 = { .device_name = "/dev/TBD", };
static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
You can't refer to a variable like tuner_614L8 until you've defined or declared it. You shouldn't try to make a non-array into an array, either. You do need to take the address of the tuner, too. You don't show DIM, but I'm assuming it is more or less one of these two equivalent macros:
#define DIM(x) (sizeof(x)/sizeof(*(x)))
#define DIM(x) (sizeof(x)/sizeof((x)[0]))
On further analysis, your tuner structure contains a flexible array member. You can't sensibly allocate such variables as static or global variables, or as automatic variables; you have to allocate them with malloc() and relatives to get a non-empty array.
However, with that caveat in mind, this code compiles:
typedef enum Radio_types { C614L8 } Radio_types;
enum { tuned = 5 };
typedef struct tuner_s
{
char *device_name;
int frequency[tuned];
int power;
int dial_lamp;
int fd[];
} tuner;
typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;
struct radio_s_614L8
{
loopsw_614L8 loop_sw_614L8;
modesw_614L8 mode_sw_614l8;
int sw_band;
int sw_bfo;
int meter;
tuner *Tuner;
int tuners;
};
static tuner tuner_614L8 = { .device_name = "/dev/TBD", };
static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
struct rss_s
{
Radio_types device_type;
char *device_info;
char *device_model;
char *device_serial;
int power_48v;
int power_400hz;
int panel_lamps;
void *radio_info;
};
struct rss_s radios[] =
{
{ C614L8, "ADF", "614L8", "8384", 0, 0, 0, &radio_614L8, },
};

Resources