mapping structures in struct C - c

im trying to index number of struct under one struct.
im tying to pass the data in the first struct to the struct pointer but i get return null.
my code is :
struct complex{
char * rNum; /* real number */
char *iNum; /* imaginary number*/
};
struct complex A = {"0","0"};
struct complex B = {"0","0"};
struct complex C = {"0","0"};
struct complex D = {"0","0"};
struct complex E = {"0","0"};
struct complex F = {"0","0"};
struct mapping{
char *key;
struct complex *P;
} complex_map [] = {
{ "A", &A },
{ "B", &B },
{ "C", &C },
{ "D", &D },
{ "E", &E },
{ "F", &F },
};
char call_complex(const char *name) {
int i;
for (i = 0; i < (sizeof(complex_map) / sizeof(complex_map[0])); i++) {
if (!strcmp(complex_map[i].key, name) && complex_map[i].P->rNum) {
complex_map[i].P->rNum;
return 0;
}
}
printf("Invalid\n");
}
and my call function is :
void read_comp(char *str){
printf(" %s",call_complex(str));
}
when i run this code i get return (null)
why?
thanks for helping

Try This:
#include <stdio.h>
struct complex{
char * rNum; /* real number */
char *iNum; /* imaginary number*/
};
struct complex A = {"1","0"};
struct complex B = {"2","0"};
struct complex C = {"3","0"};
struct complex D = {"4","0"};
struct complex E = {"5","0"};
struct complex F = {"6","0"};
struct mapping{
char *key;
struct complex *P;
} complex_map [] = {
{ "A", &A },
{ "B", &B },
{ "C", &C },
{ "D", &D },
{ "E", &E },
{ "F", &F },
};
char call_complex(const char *name)
{
int i;
for (i = 0; i < (sizeof(complex_map) / sizeof(complex_map[0])); i++)
{
if (!strcmp(complex_map[i].key, name) && complex_map[i].P->rNum)
{
return *(complex_map[i].P->rNum); // Correction
}
}
printf("Invalid\n");
}
int main()
{
printf("Got: %c \n",call_complex("A")); // Just example
return 0;
}
Thanks.

Related

Pointers to a struct inside a struct

I have two structures (one_d and two_d).
I have a function that will take struct two_d *smg as input. In main(), I am trying to create such smg so it will return value c increased.
My problem is that, while creating an array of struct two_d smg[2], I am not sure how to put inside information about its values, as it is a pointer to a different struct.
So how do you use pointer to a struct inside a struct? I would like to create struct two_d smg[2] but i dont now how to deal with struct one_d *a field in it
#include <stdio.h>
enum sid
{
DRB,
DRA,
};
struct one_d
{
unsigned int r;
unsigned int *p;
};
struct two_d
{
struct one_d *a;
enum sid z;
};
unsigned int getSmg(struct two_d *smg)
{
unsigned int c = 0;
const struct two_d *sd = NULL;
const struct one_d *ed = NULL;
for (sd = smg; sd->a != NULL; ++sd)
{
for (ed = sd->a; ed->p != NULL; ++ed)
{
if (DRA == sd->z)
{
/*P Increment the clear-state buffer size */
c += 1 + ed->r;
}
}
}
return c;
}
int main(void)
{
unsigned int rVal = 0;
struct two_d smg[2]={
//
// [0].a ={1,0},
// [0].z =DRA,
// [1].a={1,0},
// [1].z =DRA,
};
rVal = getSmg(smg);
printf("Return value is a %d\n", rVal);
printf("Return value is a l");
return( 0 );
}
Well, at least this compiles... I'm not game to run it, though...
For what it's worth...
enum sid { DRB, DRA, DRAwhoCares };
typedef struct {
unsigned int r;
unsigned int *p;
} oneD_t;
typedef struct {
oneD_t *a;
enum sid z;
} twoD_t;
unsigned int getSmg( twoD_t *smg ) {
unsigned int c = 0;
for( twoD_t *sd = smg; sd->a != NULL; +sd++ ) {
for( oneD_t *ed = sd->a; ed->p != NULL; ed++ ) {
if( DRA == sd->z ) {
/*P Increment the clear-state buffer size */
c += 1 + ed->r;
}
}
}
return c;
}
int main( void ) {
oneD_t foo[] = { { 1, NULL }, /* ... */ };
oneD_t bar[] = { { 1, NULL }, /* ... */ };
twoD_t smg[]={
{ foo, DRA, },
{ bar, DRA, },
{ NULL, DRAwhoCares, },
};
unsigned int rVal = getSmg( smg );
printf( "Return value: %u\n", rVal );
return 0; // return is not a function call... No parenthesis...
}

How do I return one of two types in C?

Suppose this code
typedef struct A {
...
} A;
typedef struct B {
...
} B;
// If it was TypeScript I would say `type uknown = A | B;`
uknown getAorB(int k) {
if (k > 0) return (A){...};
return (B){...};
}
That function getAorB should return either A or B depending on the parameter k. OK, but what is the return type and is it possible to achieve that in C?
Use unions.
typedef struct A {
...
} A;
typedef struct B {
...
} B;
typedef union
{
struct A a;
struct B b;
}A_OR_B;
// If it was TypeScript I would say `type uknown = A | B;`
A_OR_B getAorB(int k) {
A_OR_B c;
if (k > 0) c.a.member = something;
else c.b.member = somethingelse;
return c;
}
One way to do this would be to have another struct which contains the 'type'
of the returned struct. Here is what this may look like:
#define STRUCTA 1
#define STRUCTB 2
typedef struct SUPER {
int type;
} SUPER;
typedef struct A {
int type;
...
} A;
typedef struct B {
int type;
...
} B;
SUPER* getAorB(int k) {
if (k > 0) {
A *a;
a = malloc(sizeof(*a));
a->type = STRUCTA;
return (SUPER*)a;
}
B *b;
b = malloc(sizeof(*b));
b->type = STRUCTB;
return (SUPER*)b;
}
Then in the calling function you check the type of the SUPER and cast it to the appropriate function.
A *a;
B *b;
if (returnedSuper->type == STRUCTA) {
a = (A*)returnedSuper;
}
else if (returnedSuper->type == STRUCTB) {
b = (B*)returnedSuper;
}

Othello Minimax Algorithm not working

I am trying to make a minimax algorithm, but my function does not return the correct position. It returns the deepest node. I would like it to return the best possible move. Here is my code:
pos minimax(game* g, strategy_config sc)
{
int points = 0;
sc.minimax_config.heuristic(g, sc.minimax_config.hc);
pos p, p2;
int search_depth = sc.minimax_config.ply;
for (p.r = 0; p.r < g->b->nrows; p.r++)
{
for (p.c = 0; p.c < g->b->ncols; p.c++)
{
game* copy = g;
apply_move(copy, p);
if (sc.minimax_config.heuristic(copy, sc.minimax_config.hc)
> points && sc.minimax_config.ply > 0)
{
if (sc.minimax_config.ply == search_depth)
{
p2 = p;
}
sc.minimax_config.ply = sc.minimax_config.ply - 1;
minimax(copy, sc);
}
}
}
return p2;
}
And here are the relevant structs:
struct edge_corner_weight {
unsigned int edge_weight;
unsigned int corner_weight;
};
union heuristic_config {
unsigned int edge_weight;
struct edge_corner_weight edge_corner_weight;
};
typedef union heuristic_config heuristic_config;
struct minimax_config {
int (*heuristic)(game*, heuristic_config);
heuristic_config hc;
unsigned int ply;
};
typedef struct minimax_config minimax_config;
union strategy_config {
minimax_config minimax_config;
};

sizeof reporting wrong value

I'm trying to get a "column" elements count from my own structure using sizeof(_test.header.columns)/sizeof(struct _column). Unfortunately I'm always getting it as 0, because the sizeof(_test.header.columns) is always 4. Here is the code:
struct _column{
char title[40];
int length;
};
struct test_struct{
struct{
struct _column* columns;
}header;
struct{
struct _column* columns;
}details;
struct{
struct _column* columns;
}end;
};
struct test_struct _test = {
.header = {
.columns = {
{
"a",
1,
},
{
"ab",
2,
},
},
},
.details = {
.columns = {
{
"b",
2,
},
{
"bc",
3,
},
},
},
.end = {
.columns = {
{
"c",
3,
},
{
"cd",
4,
},
},
},
};
void testme(){
char buff[20];
itoa(sizeof(_test.header.columns)/sizeof(struct _column),buff,sizeof(buff));
MessageBoxA(NULL,buff,NULL,NULL);
}
please help me to resolve the problem, thanks. Any help would be appreciated.
You might try to do an approach like the following in which you do the same kind of initialization however you include as part of it the column count itself.
struct _column{
char title[40];
int length;
};
struct test_struct{
struct{
struct _column* columns;
int nColumns;
}header;
struct{
struct _column* columns;
int nColumns;
}details;
struct{
struct _column* columns;
int nColumns;
}end;
};
struct _column headerColumns [] = {
{
"a",
1
},
{
"ab",
2
}
};
struct _column detailColumns[] = {
{
"b",
2
},
{
"bc",
3
},
};
struct _column endColumns [] = {
{
"c",
3
},
{
"cd",
4
}
};
struct test_struct _test = {
{ headerColumns, sizeof(headerColumns)/sizeof(headerColumns[0])},
{ detailColumns, sizeof(detailColumns)/sizeof(detailColumns[0])},
{ endColumns, sizeof(endColumns)/sizeof(endColumns[0])}
};
The reason it fails is that you're checking the sizeof on a pointer, which returns the size of the pointer, NOT of the actual Array the memory address points to.
If you know the maximum length of the array, you can declare it as such:
_column[ integerSize ]
But then if you knew the size, you wouldn't be querying it using sizeof, I'd think ;-) You could extend the struct by adding another property of an int type that describes the size of the columns array ?

Accessing certain elements in an Struct in C

So I have the following struct I created:
struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructions[] = { { "lw", "100011" }, { "sw", "101011" }, { "beq",
"000100" } };
typedef struct _I_TypeInstructions I_TypeInstructionsStruct;
If I have a new instructionName and I want to check if it is in the I_TypeInstructionsStruct how do I iterate through just the *instructionName part of the struct above. For example the function I want to write would look something like
bool checkIfInstructionIsI_Type(char *instructionName) {
// somehow iterate through instructionNames in the struct above
// checking if parameter char *instructionName in this method is equal to
// "lw" "sw" "beq" but skipping over the binary numbers.
}
Searching a list of structs is rather straight forward:
bool checkIfInstructionIsI_Type(char *instructionName)
{
for (int i = 0; i<NumInstructions; i++)
{
if (strcmp(I_TypeInstructions[i].instructionName, instructionName) == 0)
{
return true;
}
}
return false;
}
int i;
for(i = 0; i < 3; ++i)
{
if (strcmp(instructions[i].instructionName, instructionName) == 0)
{
printf("Match found");
}
}
It's generally more useful to return the actual element that matches your string. It's the same amount of work anyway.
Add an empty element to the end of your array and then you have a end marker.
typedef struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructionsStruct;
I_TypeInstructionsStruct I_TypeInstructions[] = {
{ "lw", "100011" },
{ "sw", "101011" },
{ "beq", "000100" },
{ 0, 0}
};
I_TypeInstructionsStruct *find_instruction(char *name)
{
I_TypeInstructionsStruct *i ;
for (i = I_TypeInstructions ; i->instructionName ; i++)
if (!strcmp(i->instructionName,name)) return i ;
return 0 ;
}

Resources