Nested structure with the same name in C - c

I have a exercice that I have to realise a structure to match this line in C
int main() {
t_foo foo;
foo.foo.foo = 0;
return (0);
}
I tried to create a nested structure, but I can't create with a same name. Have you an idea ?

struct a
{
int foo;
};
struct c
{
int d;
struct a foo;
};
int main(void)
{
struct c foo;
foo.foo.foo = 2;
printf("%d\n",foo.foo.foo);
return 0;
}

For example you can do that the following way
#include <stdio.h>
typedef struct foo
{
struct
{
int foo;
} foo;
} t_foo;
int main(void)
{
t_foo foo;
foo.foo.foo = 0;
printf( "%d\n", foo.foo.foo );
return 0;
}
Or
#include <stdio.h>
typedef struct
{
struct foo
{
int foo;
} foo;
} t_foo;
int main(void)
{
t_foo foo;
foo.foo.foo = 0;
printf( "%d\n", foo.foo.foo );
return 0;
}

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;
}

Is it possible in C to have a struct or union of functions?

Is there any way, whether union, struct, or something else, to have a group of functions?
typedef struct {
//ERROR
int sqr(int i) {
return i * i;
}
//ERROR
int cube (int i) {
return i * i * i;
}
} test;
Fields in structs can be function pointers:
struct Interface {
int (*eval)(int i);
};
You cannot define the functions in the struct body, but you can assign functions with the same signature to the struct fields:
int my_sqr(int i) {
return i * i;
}
int my_cube(int i) {
return i * i * i;
}
struct Interface squarer = { my_sqr };
struct Interface cuber = { my_cube };
Then call the fields like a normal function:
printf("%d\n", squarer.eval(4)); // "16"
printf("%d\n", cuber.eval(4)); // "64"

printf shows wrong output, strange question mark at the end of line [C]

This code trying to perform queue, but that's queue has two fields: number and word. My problem is that field "word" prints incorrectly(field "number" is fine)
Expected output:
22
abc
12
efg
654
xyz
Unfortunately output looks like this
https://ibb.co/gjF446F
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}
I think that printf("%s\n",n.word) is not work correctly. But if I dont use struct, all works properly.
You need to allocate memory for word. For example like this:
typedef struct{
int number;
char word[100];
} data;
Better way is to allocate memory for word dynamically.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[100];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}

How to return the most used id of struct

I have this struct and I am trying to come up with an algorithm that returns the IDcli that was used most.
In the image below getCli() would return 33
typedef struct
{
int ID;
int IDcli;
char Name[50];
} Example;
Example e[5][5];
int getCli() {
int i=0,ID=0;
for(i=0;i<5;i++){
if(e[i][0].IDcli>0)
/*
each time it passes on same IDcli it increments
but the IDcli isnt constant
*/
}
return ID;
}
#include <unordered_map>
#include <algorithm>
using namespace std;
typedef struct
{
int ID;
int IDcli;
char Name[50];
} Example;
Example e[5][5];
int getCli() {
int i=0,ID=0;
unordered_map<int, int> m;
for (; i<5; ++i) {
for (int j=0; j<5; ++j) {
if (m.find(e[i][j].IDcli) != m.end()) {
m[e[i][j].IDcli]++;
} else {
m[e[i][j].IDcli] = 1;
}
}
}
for (unordered_map<int, int>::iterator it=m.begin(); it!=m.end(); ++it) {
ID = max(ID, it->first);
}
return ID;
}

Resources