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;
}
Related
I have this code which is basically some logical gates and I want the user to give the input values and just the program to print the output.The problem is I wnat two gates to have the same inputs and so I want to give the same values to funcs_ptr[3]->in2 and
funcs_ptr[4]->in1.
#include <stdio.h>
#include <stdlib.h>
typedef int (*CallBack)(int, int);
int myand (int a, int b);
int myor(int a, int b);
int mynand (int a, int b);
int mynor(int a, int b);
int myxor(int a, int b);
typedef struct gate
{
CallBack f;
struct gate * in1 ;
struct gate * in2 ;
} Gate;
int getinput()
{
int x;
scanf("%d", &x);
return x;
}
Gate * creategate(CallBack f)
{
Gate * temp ;
temp = malloc(sizeof (Gate));
temp->f = f;
temp->in1 = NULL;
temp->in2 = NULL;
return temp;
}
int eval(Gate *x)
{
int a, b;
if (x->in1 != NULL)
a = eval(x->in1);
if (x->in2 != NULL)
b = eval(x->in2);
if (x->in1==NULL && x->in2 == NULL)
return (x->f)(0,0);
else
return (x->f)(a,b);
}
int main( )
{
int i;
CallBack funcs[] = {mynor, myand, myor, mynand, myxor};
Gate * funcs_ptr[6 ], * inputs_ptr[6];
for(i=0;i<5;i++)
funcs_ptr[i] = creategate(funcs[i]);
for(i=0;i<6;i++)
inputs_ptr[i] = creategate(getinput);
for(i=0;i<3;i++)
{
funcs_ptr[i]->in1 = inputs_ptr[i];
funcs_ptr[i]->in2 = inputs_ptr[i+1];
}
funcs_ptr[3] = creategate(funcs[3]);
funcs_ptr[3]->in1 = funcs_ptr[0];
/*--*/funcs_ptr[3]->in2 = funcs_ptr[1];
funcs_ptr[4] = creategate(funcs[4]);
/*--*/funcs_ptr[4]->in1 = funcs_ptr[1];
funcs_ptr[4]->in2 = funcs_ptr[2];
funcs_ptr[5] = creategate(funcs[3]);
funcs_ptr[5]->in1 = funcs_ptr[3];
funcs_ptr[5]->in2 = funcs_ptr[4];
printf(">>>%d\n", eval(funcs_ptr[5]));
return 0;
}
int myand (int a, int b)
{
return a * b;
}
int myor(int a, int b)
{
return a+b>0;
}
int mynand (int a, int b)
{
return myand(a,b)==0;
}
int mynor(int a, int b)
{
return myor(a,b)==0;
}
int myxor(int a, int b)
{
if(a!=b) return 1;
return 0;
}
But when I run the code the eval function calls getinput() two times instead of just passing the same value.
How can I pass the same value to both structs?
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);
}
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));
}
I have to write a normal sum function and a reentrant one in C. I have to pass a int and it have to be addedd to a INIT_VALUE. In the reentrant function the main pass a int* to keep the state. How can i initialize this pointer on the first call? I have to initialize it in the fun, not in the main. Thanks
#include <stdio.h>
#ifndef INIT_VALUE
#define INIT_VALUE 0
#endif
int somma(int x){
static int val = INIT_VALUE;
val += x;
return val;
}
int somma_r(int x, int* saveptr){
// pointer initialize and sum
// return old_value ;
}
int main (){
int x;
int s;
int s_r;
int *stato;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}
With the function signature in your program (int somma_r(int x, int* saveptr)) you cannot initialize the pointer on the first call.
You probably need this (3 lines of your code modified):
...
int s = INIT_VALUE; // otherwise s will not be initialized
int s_r = INIT_VALUE; // otherwise s_r will not be initialized
int stato = INIT_VALUE; // state to be used with the somma_r function
...
s_r = somma_r(x, &stato);
...
somma_r function
int somma_r(int x, int* saveptr){
*saveptr += x;
return *saveptr;
}
Version with initialisation inside the somma_r function. This requires a modification of the signature of somma_r:
int somma_r(int x, int **saveptr){
if (*saveptr == NULL) {
*saveptr = malloc(sizeof(int));
**saveptr = INIT_VALUE;
}
**saveptr += x;
return **saveptr;
}
int main (){
int x;
int s = 0;
int s_r = 0;
int *stato = NULL;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,&stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}
Simple question: Is there a way to determine the stack size of a function?
int stackframe_size(int run) {
int i ;
if(!run) {
return ((int)(&i) - stackframe_size(++run));
}
return (int)(&i);
}
int main() {
int x, y;
double d;
char c;
int a = 4;
int b = 5;
int we = 6;
int e = 123123;
int hmm = 34453;
int lol = 45;
int asd = 23;
x = 1;
y = g(x);
d = f(x, y, x-y);
c = 'a';
printf("%d", stackframe_size(0));
}
I am running the function I obtained from another thread to find the call stack size and it always seems to return 48...is there another way to find out or is this the only way?