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;
}
Related
I'm trying to change the x value in this code but I'm getting segmentation fault.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int **x;
} Type;
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x[0] = &a;
return 0;
}
if you want an array of pointers to ints
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x = malloc(sizeof(int*) * 10)) ;// say we need 10
type->x[0] = &a;
return 0;
}
I managed to put the value in the pointer while in the function, However when i come back to the main i just dont get the values. Where am i wrong? sending parameters wrong? wrong allocation? Here's the code:
bool wc(int* nlines, int* nwords, int* nchars)
{
int lines=5,chars=6,words=7;
nchars = (int *) malloc(chars*sizeof(int));
*nchars = chars;
nlines = (int *) malloc(lines*sizeof(int));
*nlines = lines;
nwords = (int *) malloc(words*sizeof(int));
*nwords = words;
}
int main() {
int* chars; int* words; int* lines;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",chars,lines,words);
return 0;
}
If all you want to do is be able to set 3 int values inside a function then this is how I would so it.
#include <stdio.h>
#include <stdbool.h>
bool wc(int* nlines, int* nwords, int* nchars)
{
int lines=5,chars=6,words=7;
*nchars = chars;
*nlines = lines;
*nwords = words;
return true;
}
int main() {
int lines = 0;
int words = 0;
int chars = 0;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",chars,lines,words);
return 0;
}
If for some reason you must use pointers as shown in your example then this will do what you want.
#include <stdio.h>
#include <stdbool.h>
bool wc(int** nlines, int** nwords, int** nchars)
{
int lines=5,chars=6,words=7;
*nchars = malloc(sizeof(int));
**nchars = chars;
*nlines = malloc(sizeof(int));
**nlines = lines;
*nwords = malloc(sizeof(int));
**nwords = words;
return true;
}
int main() {
int* chars; int* words; int* lines;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",*chars,*lines,*words);
free(chars);
free(words);
free(lines);
return 0;
}
As you can see this just means you need to add a bunch more * all over the place.
In C function input variables are passed by value, not reference. So when you assign them locally, the value in the caller scope is unaffected. E.g.
void foo(int a) {
a = 5;
}
int main() {
int b = 3;
foo(b);
// here, b is still 3
}
This is exactly what you are doing in your example, though your variables are not int, but int*.
If your input variable is a pointer though, you can change the memory that the variable points to, and this will obviously reflect in the calling scope. E.g.
void foo(int *a) {
*a = 5;
}
int main() {
int b = 3;
foo(&b);
// here, b is 5
}
In your case, you want to allocate pointers, so you want your function signature to be a pointer to a pointer. E.g.
void foo(int **a) {
*a = malloc(sizeof(int));
}
int main() {
int* b = NULL;
foo(&b);
// here, b is allocated to a valid heap area
free(b);
}
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;
}
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);
}
int* sum(int *mypt,int len){
int i;
int mysum = 0;
for(i=0;i<len;i++)
mysum += mysum;
return &mysum; // I cannot do this since the function frame goes away after the call.
}
int main()
{
int *pt = (int*) malloc(3*sizeof(int));
pt[0] = 0;
pt[1] = 1;
pt[2] = 2;
int *s = sum(pt,3);
printf("%d\n",*s);
return 0;
}
I would like to return a pointer to the mysum. I cannot do int static sum = 0 because it is fixed. I cannot use int const sum = 0 because it gives an error ""readonly"". Is there a solution to this ?
why do you need to return a pointer from sum() you can just return the answer by value. If you must return a pointer, than you will need to malloc() the memory inside of sum() and the caller of sum will have to free() the answer, but that is not very efficient.
int sum(int mypt, len)
{
int i;
int mysum = 0;
for(i=0;i<len;i++)
mysum += mysum;
return mysum; // return the answer by value.
}
And change main() as below:
int main()
{
int *pt = (int*) malloc(3*sizeof(int));
pt[0] = 0;
pt[1] = 1;
pt[2] = 2;
int s = sum(pt,3);
printf("%d\n",s);
return 0;
}
Yes, use malloc to place the integer in heap memory and obtain a pointer. You can then return this pointer from the function:
int* sum(int *mypt, int len) {
int i;
int* mysum = malloc(sizeof(int));
//make sure you dereference (*) when you wish to work with the value.
*mysum = 0;
for(i=0; i<len; i++) *mysum += *mysum;
return mysum;
}
Aside, it looks like a lot of your code is broken. This only solves how to return the pointer!