-1

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;
}
SimC
  • 105
  • 2
  • 8
  • _I have to initialize it in the fun, not in the main_. No. Why? – Jabberwocky Mar 11 '16 at 13:22
  • Use a pointer to pointer and use [malloc](http://www.tutorialspoint.com/c_standard_library/c_function_malloc.htm) inside your func. E.g. `int somma_r(int x, int **saveptr){*saveptr=malloc(sizeof(int);}` – LPs Mar 11 '16 at 13:24

1 Answers1

3

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;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I cannot resist: [Do I cast malloc return?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)....I'm joking... ;) – LPs Mar 11 '16 at 14:48
  • ;) Yes of curse.. I read (if I'm not wrong) your discussion with Olaf some time ago. – LPs Mar 11 '16 at 15:45
  • I have to use somma_r like strtok_r. When i call strtok_r i use a pointer and i set not it NULL – SimC Mar 11 '16 at 16:48
  • @user3357296 `strtok_r` is quite different from your `somma_r` function. From the `strtok_r`documentation: _On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call._. But you don't have the option of passing a NULL ptr to _somma_r_. Try to describe your `somma_r` function in a similar way as the description of `strtok_` and you will see that it is not possible. – Jabberwocky Mar 11 '16 at 19:19