I'm trying to do the following, but the compiler is complaining about brackets, however, I can't find my way to an alternative.
struct cards {
char faces[13][6], suits[4][9];
}
typedef struct cards cards;
void init_struct(cards *s) {
s->suits = {"hearts","spades","clubs","diamonds"};
s->faces = {"ace","two","three","four","five",
"six","seven","eight","nine"
"ten","jack","queen","king"};
}
I realize that there are several possible duplicate threads out there, but none of them has led me on the track. I hope one of you can :) Thanks
#include <string.h>
typedef struct cards {
char faces[13][6], suits[4][9];
} cards;
cards base_card = {
{"ace","two","three","four","five",
"six","seven","eight","nine", //forgot "," at nine after
"ten","jack","queen","king"},
{"hearts","spades","clubs","diamonds"}
};
void init_struct(cards *s) {
memcpy(s, &base_card,sizeof(cards));
}
The direct initialization syntax can only be used for initialization, not assignment. You cannot do this, for example:
char p[2][5];
p = {"a", "b"}; //error
That's why it fails to compile. Try strcpy-ing string by string
strcpy(s->suits[0], "hearts");
strcpy(s->suits[1], "spades");
...etc
or, alternatively, initialize a temporary array and then copy it
char suits_tmp[4][9] = {"hearts","spades","clubs","diamonds"};
memcpy(s->suits, suits_tmp, 4*9);
#include <string.h>
#include <stdio.h>
struct cards {
const char** suits;
const char** faces;
};
typedef struct cards cards;
const char* suits[4] = {"hearts","spades","clubs","diamonds"};
const char* faces[13] = {"ace","two","three","four","five",
"six","seven","eight","nine"
"ten","jack","queen","king"};
int main()
{
cards deck;
deck.suits = suits;
deck.faces = faces;
printf(deck.suits[0]);
return 0;
}
This works as well. Uses no pointers.
Clarification
I know mine is the quick and dirty answer, but there is no strcpy or memcpy or a long list of assignments. If your plan is to use the standard deck of cards for your game, then it would be a constant set of values anyway. If your intent is to have different types of decks, then my answer may not be adequate. Yes, it doesn't have a init_struct function, but you could easily modify it for your intent (since I am not well versed in C and malloc.)
Use const char * within your struct (I assume there is no requirement to modify the actual content of the suit/face value) and initialise them individually:
struct cards {
const char *suits[4];
const char *faces[13];
};
typedef struct cards cards;
void init_struct(cards *s)
{
s->suits[0] = "hearts";
s->suits[1] = "spades";
s->suits[2] = "clubs";
s->suits[3] = "diamonds";
s->faces[0] = "ace";
s->faces[1] = "two";
s->faces[2] = "three";
s->faces[3] = "four";
s->faces[4] = "five";
s->faces[5] = "six";
s->faces[6] = "seven";
s->faces[7] = "eight";
s->faces[8] = "nine";
s->faces[9] = "ten";
s->faces[10] = "jack";
s->faces[11] = "queen";
s->faces[12] = "king";
}
Of course, if you just want a one-off set of cards, which is reasonable, then this will work:
struct
{
const char *suits[4];
const char *faces[13];
} cards =
{
{"hearts","spades","clubs","diamonds"},
{"ace","two","three","four","five",
"six","seven","eight","nine",
"ten","jack","queen","king"}
};
Related
I'm learning pointers in C and i came across a confusion between pointers X struts X functions
The goal: creating two structs and mutate properties inside them.
The path I'm going: I am creating these two structs and then passing its memory addresses to the mutate function, the function then prints and mutates some properties of these structs.
Result I get:
1: The name of the struct created is nod being entirely printed and its of the wrong struct passed, and the life property is not properly changed and printed to the screen.
2: On the terminal I get "Segmentation Fault", not sure why but I'm pretty sure its something wrong I did.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
void attackHero(Hero *hero, int *power) {
(*hero).life = (*hero).life - *power;
printf("Damage: %d\n", *power);
printf("Attacked hero: %s\n", (*hero).name);
printf("Hero's remaining life: %d\n", (*hero).life);
};
int main () {
Hero flash;
flash.power = 250;
flash.life = 500;
strcpy(flash.name, "The Flash");
Hero batman;
batman.power = 380;
batman.life = 700;
strcpy(batman.name, "Batman arkham knight");
attackHero(&flash, &batman.power);
return 0;
}
Result printed to the terminal (Vscode + gcc):
Here is the warning that I get when I compile your original code:
1.c:25:2: warning: ‘__builtin_memcpy’ writing 10 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
25 | strcpy(flash.name, "The Flash");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.c:30:2: warning: ‘__builtin_memcpy’ writing 21 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
30 | strcpy(batman.name, "Batman arkham knight");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to use the flexible array then you have to allocate space for it like this:
int main () {
Hero *flash = malloc(sizeof(*flash) + sizeof("The Flash"));
flash->power = 250;
flash->life = 500;
strcpy(flash->name, "The Flash");
Hero *batman = malloc(sizeof(*flash) + sizeof("Batman arkham knight"));
batman->power = 380;
batman->life = 700;
strcpy(batman->name, "Batman arkham knight");
attackHero(flash, &batman->power);
free(flash);
free(batman);
}
Here there the resulting code refactored a bit, and I added a error check for malloc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
Hero *createHero(int power, int life, const char *name) {
Hero *h = malloc(sizeof(*h) + strlen(name) + 1);
if(!h) {
printf("malloc failed\n");
exit(1);
}
h->power = power;
h->life = life;
strcpy(h->name, name);
return h;
}
void attackHero(Hero *hero, int power) {
hero->life -= power;
printf(
"Damage: %d\n"
"Attacked hero: %s\n"
"Hero's remaining life: %d\n",
power,
hero->name,
hero->life
);
};
int main(void) {
Hero *flash = createHero(250, 500, "The Flash");
Hero *batman = createHero(380, 700, "Batman arkham knight");
attackHero(flash, batman->power);
free(flash);
free(batman);
}
Alternatively use a fixed array (char [64] as suggested by #Diego) or a char * and allocate space to it. The former only needs 2 lines of code change from the original:
// largest name in use
#define NAME_LEN sizeof("Batman arkham knight")
typedef struct {
int power;
int life;
char name[NAME_LEN];
} Hero;
Whole lotta malloc() going on. Since the hero’s names are string literals (and assuming they don’t change), just change name[]; to const char *name in the structure and initialize via simple assignment:
flash.name = "The Flash";
batman.name = "Batman arkham knight";
No worries about malloc() failures, name sizes or free() requirements.
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)();
}
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently I select language at compile time, as below:
#define FRENCH_LANG 0
#define ZULU_LANG 0
#define ENGLISH_LANG 1
#if(FRENCH_LANG == 1)
const char PROMPT_HELLO[] = "Bonjour";
const char PROMPT_THANKS[] = "Merci";
#elif(ZULU_LANG == 1)
const char PROMPT_HELLO[] = "Sawubona";
const char PROMPT_THANKS[] = "Ngiyabonga";
#elif(ENGLISH_LANG == 1)
const char PROMPT_HELLO[] = "Hello";
const char PROMPT_THANKS[] = "Thanks";
#endif
int main(int argc, char *argv[]) {
printf("%s\r\n", PROMPT_HELLO);
printf("%s\r\n", PROMPT_THANKS);
return 0;
}
I would like to change my code so that all languages are compiled in and the user can select the desired language.
Is there a smart way to do this so that I do not have to duplicate or triplicate my printf's?
As a variant of Some programmer dude's answer, you can go with structures and a global pointer that keeps the current language. No need for the enumeration then, and easier access by dropping one level of indirection:
struct strings {
const char *hello;
const char *thanks;
};
const struct strings strings_english = {
.hello = "Hello",
.thanks = "Thanks",
};
const struct strings strings_zulu = {
.hello = "Sawubona",
.thanks = "Ngiyabonga",
};
const struct strings_french = {
.hello = "Bonjour",
.thanks = "Merci",
};
static const struct strings *current_strings = NULL;
void set_language(const char *code)
{
if(strcmp(code, "en") == 0)
current_strings = &strings_english;
...
}
int main(void)
{
set_language("en");
printf("%s\n", current_strings->hello);
return 0;
}
There are of course many things to improve here, but it can be decent. Repeating the member names (.hello = and so on) in the initializers makes it a bit easier to verify, and might also make life easier if/when you take the step to build tools to extract/update the text. Sometimes translation is not done by programmers, and that requires a way to get text in and out of the program.
You could do it using an array of arrays of strings:
// Define the language indexes
#define FRENCH_LANG 0
#define ZULU_LANG 1
#define ENGLISH_LANG 2
// Number of strings translated
#define NUMBER_OF_STRINGS 2
// The translated strings
#define PROMPT_HELLO 0
#define PROMPT_THANKS 1
// Define the strings
const char *languages[3][NUMBER_OF_STRINGS] = {
// French
{
"Bonjour",
"Merci"
},
// Zulu
{
"Sawubona",
"Ngiyabonga"
},
// English
{
"Hello",
"Thanks"
}
};
Then with a variable set to one of the language index you could do e.g.
int language = ENGLISH_LANG;
...
printf("%s", languages[language][PROMPT_HELLO]);
Another possible solution, very similar to the above, is to use an array of structures, where each member of the structure is a string for the language:
struct translation_strings
{
const char *hello;
const char *thanks;
};
// Define the language indexes
#define FRENCH_LANG 0
#define ZULU_LANG 1
#define ENGLISH_LANG 2
struct translation_strings languages[3] = {
// French
{
"Bonjour",
"Merci"
},
// Zulu
{
"Sawubona",
"Ngiyabonga"
},
// English
{
"Hello",
"Thanks"
}
};
...
printf("%s", languages[language].hello);
Do note that this will take up a lot of space if you have lots of strings you want to have in your application. It will effectively triple the data size of your program.
If the target is a larger embedded system with possible filesystem then there might be better ways to handle translations. For example if you're targeting a Linux system then I rather suggest you look into the GNU gettext system for handling translations (it is the de-facto standard on Linux-based systems).
typedef enum
{
e_FRENCH = 0,
e_ZULU = 1,
e_ENGLISH = 2
} language;
const char* greetings[] = {
"Bonjour", /* french */
"Sawubona", /* zulu */
"Hello", /* english */
};
void greet_in(language l)
{
printf("%s", greetings[(int) l]);
}
int main()
{
greet_in(e_ENGLISH); // prints "Hello"
return 0;
}
You can then read a value for language from a configuration file or stdin.
If you do not want to change all your code, but only the initialization, you can use char pointers. The pointers have the same name as the consts had before. Of course it is possible to use smarter initialization, this is just an example.
lang.h
------
extern const char *PROMPT_HELLO;
extern const char *PROMPT_THANKS;
void set_lang(int lang);
lang.c
------
const char *PROMPT_HELLO;
const char *PROMPT_THANKS;
void set_lang(int lang)
{
if (lang==FRENCH_LANG)
{
PROMPT_HELLO = "Bonjour";
PROMPT_THANKS = "Merci";
}
else if (lang==ZULU_LANG)
{
PROMPT_HELLO = "Sawubona";
PROMPT_THANKS = "Ngiyabonga";
}
else if (lang==ENGLISH_LANG)
{
PROMPT_HELLO = "Hello";
PROMPT_THANKS = "Thanks";
}
}
Finally, the program code is not changed:
int main(int argc, char *argv[]) {
set_lang(ZULU_LANG);
printf("%s\r\n", PROMPT_HELLO);
printf("%s\r\n", PROMPT_THANKS);
return 0;
}
what is wrong with the following code?
parseCounter1() and parseCounter1() below are two functions.
I put their pointers in const OptionValueStruct so that
they can be called accordingly when each element of option_values[]
are gone through:
typedef struct OptionValueStruct{
char counter_name[OPTION_LINE_SIZE];
int* counter_func;
} OptionValueStruct_t;
const OptionValueStruct option_values[] = {
{"Counter1", (*parseCounter1)(char*, char**)},
{"Counter2", (*parseCounter2)(char*, char**)},
};
const OptionValueStruct *option = NULL;
for(int i = 0; i< sizeof(option_values)/sizeof(OptionValueStruct_t); i++){
option = option_values + i ;
result = option->counter_func(opt_name, opt_val);
}
You have declared your counter_func member to be a pointer to an int, not a function pointer , while you have something resembling a function pointer declaration in your option values. Here's what you want (assuming your return type is int )
typedef struct OptionValueStruct{
char counter_name[OPTION_LINE_SIZE];
int (*counter_func)(char*, char**);
} OptionValueStruct_t;
const OptionValueStruct_t option_values[] = {
{"Counter1", parseCounter1},
{"Counter2", parseCounter2},
};
for(int i = 0; i< sizeof(option_values)/sizeof(OptionValueStruct_t); i++){
result = option_values[i]->counter_func(opt_name, opt_val);
// don't know what you relly want to do with result further on..
}
If you are compiling as C code (as your tag suggests), then you should change the type of option_values[] and option to OptionValueStruct_t. In C++, however, this is OK.
Alternatively, you can eliminate the trailing _t from the custom type name.