Passing a struct member NAME to function in C? - c

Is there any simple ways to pass the name of a struct member to a function in C? For example if I want to make this happen:
(I know the code is incorrect, I just wrote it to explain the question)
struct Test
{
int x;
int y;
};
int main()
{
struct Test t;
t.x = 5;
t.y = 10;
example(t, <MEMBER NAME>);
}
void example(struct Test t, <MEMBER NAME>)
{
printf("%d", t.<MEMBER NAME>);
}

Not sure if this is exactly what you are looking for but here is a pretty close solution using offsetof:
struct Test
{
int x;
int y;
};
void example(void *base, size_t offset)
{
int *adr;
adr = (int*)((char*)base + offset);
printf("%d\n", *adr);
}
int main(int argc, char **argv)
{
struct Test t;
t.x = 5;
t.y = 10;
example(&t, offsetof(struct Test, y));
}

Related

Adding new item to an array of structs in C

I have some structs as following:
typedef struct {
char debutAge[15];
char finAge[15];
} Age;
typedef struct {
char type[15];
char composants[50];
Age utilisation;
} Categorie;
typedef struct {
int code;
char nom[30];
float prix;
Categorie med;
int quantitie;
} Medicament;
#define MAX 100
typedef Medicament Pharmacie[MAX];
Pharmacie P;
int nb=0;
In my main function, I'm trying to add a new element to the Pharmacie P array, this is what I tried:
void main()
{
Medicament m = Saisir();
ajouterMedicament(P, nb, m);
afficherMedicaments();
}
int ajouterMedicament(Pharmacie *ph, int *nb, Medicament m) {
int i;
for (i = 0; i < *nb; i++) {
if (m.code == ph[i].code) {
ph[i].prix = m.prix;
ph[i].quantitie = m.quantitie;
}
return 1;
}
if (*nb < MAX) {
ph[*nb] = m;
*nb += 1;
return 1;
}
return 0;
}
But I'm getting an error on this line: mif (m.code == ph[i].code) { :
expression must have struct or union type
How can I solve this ?
You don't need to declare the first argument as Pharmacie *. Pharmacie is a typedef for an array, so you don't need to add *.
int ajouterMedicament(Pharmacie ph, int *nb, Medicament m) {
And in the call, you need to pass a pointer to nb so it can be updated:
ajouterMedicament(P, &nb, m);
In general it gets confusing when you use typedef for pointer types, and you ran into that. I recommend not doing that. See Is it a good idea to typedef pointers?

about remove/replace struct

I have a C language code that uses struct, including functions and function calls that initialize the structure. Now I want to remove the use of structs. Due to problems with code execution, and a lot of code and complicated structs, I can't change these manually. Functions and structures, so I have to find an automated method. The following code is a simple example.
Is there any better way or idea?
#include<stdio.h>
struct A
{
int a;
int b;
};
struct A add(int x, int y)
{
struct A t;
t.a = x + y;
return t;
}
int main()
{
struct A t = add(3, 4);
printf("t.a = %ld\n", t.a);
return 0;
}
To:
#include<stdio.h>
int main()
{
int A_a = 3;
int A_b = 4;
int A_a_b = A_a + A_b;
printf("%d\n", A_a_b);
return 0;
}
Have you tried antlr?
I guess you'd like refactor the code to below.
include
/*
struct A
{
int a;
int b;
};
*/
/*
struct A add(int x, int y)
{
struct A t;
t.a = x + y;
return t;
}
*/
int main()
{
/*
struct A t = add(3, 4);
*/
int A0_t_a; //t.a
int A0_t_b; //t.b
{
//add(3, 4)
int x = 3;
int y = 4;
//struct A t;
int A1_t_a;
int A1_t_b;
//t.a = x + y
A1_t_a = x + y;
//return t
A0_t_a = A1_t_a;
A0_t_b = A1_t_b;
}
/*
printf("t.a = %ld\n", t.a);
*/
printf("t.a = %ld\n", A0_t_a);
return 0;
}

How to make a function return multiple values? [duplicate]

This question already has answers here:
How do I return multiple values from a function in C?
(8 answers)
Closed 4 years ago.
struct r() {
return 1, "hello";
}
int main() {
int x;
char y[100];
x, y = r(); // set x to int 1, and set y to "hello"
}
Is there anyway I can do this ? I believe this is possible in C
Yes, you can do this with structures, which may contain arbitrary data fields, as with the following complete program:
#include <stdio.h>
struct tPair {int one; int two;};
struct tPair returnPair(void) {
struct tPair plugh;
plugh.one = 7;
plugh.two = 42;
return plugh;
}
int main(void) {
struct tPair xyzzy = returnPair();
printf("Pair is %d, %d\n", xyzzy.one, xyzzy.two);
return 0;
}
If you compile and run that, you'll see:
Pair is 7, 42
A function cannot return multiple values.
You can however pass pointers so that a function writes the data through the
pointer:
void foo(int *x, int *y)
{
*x = 1;
*y = 2;
}
void bar(void)
{
int a, b;
foo(&a, &b);
printf("a: %d, b: %d\n", a, b); // prints a: 1, b: 2
}
Another option is to create a struct and return that struct:
struct answer {
int x;
int y;
};
struct answer foo(void)
{
struct answer a;
a.x = 1;
a.y = -4;
return a;
}
void bar(void)
{
struct answer p = foo();
printf("p.x: %d, p.y: %d\n", p.x, p.y);
}
You have to use struct, and at the same time I will suggest you get a book
#include <stdio.h>
struct Return_Value {
int x;
char *y;
};
typedef struct Return_Value Return_Value_t;
Return_Value_t r() {
Return_Value_t revals = { .x = 1, .y = "hello" };
return revals;
}
int main() {
int x;
char y[100];
Return_Value_t reval = r();
printf("%d\n", reval.x);
printf("%s\n", reval.y);
}

How do I call a function inside a struct?

I have this code:
int suma(int);
int produs(int);
struct calcul{
int suma();
int produs();
}
suma()=1+2+..n;// return S
produs()=1*2*..n;// return P
I want to call it in main with
calcul sp. How do I call function inside a struct?
If I give n a struct type n=5; the result to be sp(15,120).
Thanks!!!
I guess you want something like:
struct calcul
{
int suma(int n)
{
int result = 0;
for (int i = 1; i <= n; ++i)
result += n;
return result;
}
//... similar for produs
};
int main()
{
calcul sp;
int x = sp.suma(10);
};
int suma(int);
int produs(int);
and
struct calcul{
int suma();
int produs();
};
are two completely different sets of functions, even though they have the same name. The functions in your struct are member functions and can only be called on an instance of your struct. You would do it like so:
int main()
{
calcul x;
int a = x.suma(0);
int b = x.produs(1);
}

Getting the address of function return arguement in C

Having read the chapter about sttructures from "The C programming Language book" I tried the following code. The goal is to have an array of pointer initialized with some specific value for all its points.
#include <stdio.h>
#define MAXPOINTS 1000
struct point {
int x;
int y;
};
struct point makepoint(int x, int y);
int main(int argc, const char *argv[])
{
int i;
int number1 = 5, number2 = 10;
struct point *points[1000];
for (i=0; i< MAXPOINTS; i++) {
points[i] = &(makepoint(number1, number2));
}
}
struct point makepoint(int x, int y) {
struct point my_point;
my_point.x = x;
my_point.y = y;
return my_point;
}
The error generated after running the above code is the following:
test_something.c:18:22: error: cannot take the address of an rvalue of type 'struct point'
Why does this happen since the makepoint function does return a valid point object?
Thanks in advance,
You are returning a temporary copy of a point and take his address is not a good idea.
Try this:
struct point* makepoint(int x, int y);
int main(int argc, const char *argv[]) {
int i;
int number1 = 5, number2 = 10;
struct point* points[MAXPOINTS];
for (i=0; i< MAXPOINTS; i++)
points[i] = makepoint(number1, number2);
for (i=0; i< MAXPOINTS; i++)
free(points[i]);
return 0;
}
struct point* makepoint(int x, int y) {
struct point* my_point = malloc(sizeof(struct point));
my_point->x = x;
my_point->y = y;
return my_point;
}
Anyway, in your code:
struct point *points[10];
for (i=0; i< MAXPOINTS; i++) {
points[i] = &(makepoint(number1, number2));
}
...you have an array of 10 pointers and you're trying to assign 1000 pointers (MAXPOINTS).
You cannot take the address of a value, only of a variable. This is because values don't necessarily need to live in (addressable) memory. For example: the return value of a function is (usually) passed via a register, and you cannot take the address of a register(-variable).
You could instead change your makepoint function to take a pointer to a struct point and fill it in:
struct point makepoint(struct point * in, int x, int y){
in->x = x;
in->y = y;
return *in;
}
Note that the return value isn't strictly necessary, but kept for 'backward compatability'.

Resources