Something wrong with char arrays and pointer?

Hello,
i’m usign arduino uno with a sim808 module; i’d like to get the phone number of incoming calls and this is my sketch

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

char gprsBuffer[64]; 
char *s = NULL;

char *p1;
char *phone;
char my[] = "+39xxxxxxxxx"; // it is the default phone number
char tel[20];

// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        
// constants won't change:
long interval = 10000;           

#define PIN_TX    3
#define PIN_RX    2
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

void ring(){
//******** Wait serial data *************
   if(sim808.readable()){
      sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
      //Serial.println(gprsBuffer);
      p1 = strstr(gprsBuffer, "+39");
      if(NULL != p1) {
        //Serial.println(p1);
        //memset(pippo, '\0', sizeof(tel));
        strncpy (tel, p1, 20);
        Serial.println(p1);
        Serial.println(tel);
        //strncpy(tel,NULL, 100);
        phone = tel;
        Serial.println(phone);
      
         //p1 = strstr(gprsBuffer, " \"+ ");
          //if (p1 != NULL) {
              //Serial.print(p1);
              //p2 = strstr(p1, " \" ");
              //if (p2) Serial.print(p2);//printf("%.*s\n", p2 - p1 - 6, p1 + 6);          
         
          //}
    
      }

   //************** Detect the current state of the telephone or SMS ************************
      if(NULL != strstr(gprsBuffer,"RING")) {
          
          sim808.answer();
          
    sim808_clean_buffer(gprsBuffer,32);
   }
  }
}

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  
  phone = my;
  pinMode(13, OUTPUT);

  while(!sim808.init())
  {
      Serial.print("Sim808 init error\r\n");
   }
  delay(3000);
  
  Serial.println("Init Success, please call me!");

}


void loop() {
    //Serial.println(phone);
    unsigned long now = millis(); 
    
    while (millis() < now + interval) { 
      ring();
    }    
}

I noticed that on the first two call the number stored in *p1, tel and *phone is correctly showed, at the third it loses some numbers, from the fouth on nothing is display, is it a problem with char array or pointers in which the phone number is stored? I cannot figure out…

If "+39" is found in the buffer, "sim808_clean_buffer(gprsBuffer, 32);" is never called. Should it be called?

Thanks for your answer, I don't know if this is the issue (also the second if with buffer clean should be called during RING=call) , but I'll try to indent the first if...+39 inside the "if(NULL != strstr(gprsBuffer,"RING"))"

no way, this is the modified code

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#define MESSAGE_LENGTH 160
char gprsBuffer[64]; 
char *s = NULL;

char *p1;
char *phone;
char my[] = "+393317943514";
char tel[20];

// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        
// constants won't change:
long interval = 10000;           

#define PIN_TX    3
#define PIN_RX    2
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

void ring(){
//******** Wait serial data *************
   if(sim808.readable()){
      sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
      //Serial.println(gprsBuffer);
      

   //************** Detect the current state of the telephone or SMS ************************
      if(NULL != strstr(gprsBuffer,"RING")) {

        p1 = strstr(gprsBuffer, "+39");
        if(NULL != p1) {
          //Serial.println(p1);
          //memset(pippo, '\0', sizeof(tel));
          strncpy (tel, p1, 20);
          Serial.println(p1);
          Serial.println(tel);
          //strncpy(tel,NULL, 100);
          phone = tel;
          Serial.println(phone);
        
           //p1 = strstr(gprsBuffer, " \"+ ");
            //if (p1 != NULL) {
                //Serial.print(p1);
                //p2 = strstr(p1, " \" ");
                //if (p2) Serial.print(p2);//printf("%.*s\n", p2 - p1 - 6, p1 + 6);          
           
            //}
      
          }
          
          sim808.answer();
          
    sim808_clean_buffer(gprsBuffer,32);
   }
  }
}

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  
  phone = my;
  pinMode(13, OUTPUT);

  while(!sim808.init())
  {
      Serial.print("Sim808 init error\r\n");
   }
  delay(3000);
  
  Serial.println("Init Success, please call me!");

}


void loop() {
    //Serial.println(phone);
    unsigned long now = millis(); 
    
    while (millis() < now + interval) { 
      ring();
    }    
}

but the telephone number is showed randomly (only on first and, sometimes, second call, then nothing)

while (millis() < now + interval) {
      ring();
    }

You only call ring() for seven seconds. How many calls do you expect? Why the time limit?

it's only a test to see if i can get and store the phone number that is calling

Be careful using strncpy(), it does not append a terminating null if the source if longer than the number of characters you are copying.

it's only a test to see if i can get and store the phone number that is calling

How do you know that you receive three or more calls in that period?

Be careful using strncpy(), it does not append a terminating null if the source if longer than the number of characters you are copying.

That's a good point, but the OP sets up a 64 character buffer, and then only deals with 32 characters or less during the sketch. I think he guarantees a terminating null from the initialization.