Arduino logic check problem

#include <Wire.h>

int data_index = 0;
char ptr[256];   // 256 karakterlik sabit boyutlu bir dizi
String line_data;

void setup() {
  Wire.begin(8); // Slave adresini 8 olarak ayarla
  Wire.onReceive(drawData); 
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  delay(100);
}

// Veriyi yazdırma fonksiyonu
void yazdir(char* data) {
  line_data = data;
  Serial.println(line_data);     // Veriyi seri porta yazdır
  digitalWrite(13, HIGH);        // LED'i yanıp söndürme
  delay(50);
  digitalWrite(13, LOW);
}

// Veriyi almak için kullanılan callback fonksiyonu
void drawData(int sendersbyte) {
  while (Wire.available() > 0) {     
    char c = Wire.read();
    if (c != '\n' && data_index < 255) {  // 255'e kadar alabiliyor
        ptr[data_index++] = c;
    } else {
      if (c == '\n') {  // Eğer ENTER (yani '\n') karakteri geldiyse
        ptr[data_index] = '\0'; // Sonlandırıcı karakter ekle
        yazdir(ptr);            // Veriyi yazdir fonksiyonuna gönder
        data_index = 0;         // Indexi sıfırla
        memset(ptr, 0, 256);    // Buffer'i temizle
      }else if (c != '\n'){
        // Eğer ENTER yoksa ve sınır aşıldıysa resetle
        memset(ptr, 0, 256);
        data_index = 0;
      }
    }
  }
}

hello everybody,firstly sorry for my english.I have been coding a i2C coomunication program.I want to call yazdir() function when the data has a "\n" char so "ENTER".But It does not write anything when I send an example data like that "123123fff"(no enter).After I send a "testtest123"(there is enter at the end). The outcome is being "123123ffftesttest123". why?

You are writing data to the ptr array until you receive a '\n' so the "123123fff" will be in the buffer. You then receive "testtest123", which is added to the buffer, followed by a '\n' which causes the yazdir() function to be called. However, the ptr array still has all the data in it including the original "123123fff"

But there is "memset(ptr, 0, 256)" It will be deleted previous "ptr" if no '\n' . "ENTER"

Under what conditions will the program reach the memset() ?

hmmm.now I get it.Weel How can I do if there is no enter at the end of the sent data, that is, "\n", or if it exceeds 255 characters, delete it by memset ptr(), if there is an "ENTER" at the end and it is less than 255 characters, it can print it

You could read characters for a period and add them to the buffer. If the number of characters exceeds 255 or a '\n' is not received within the time period then assume that the message is invalid and reset the array index and start reading again

Do you have control over the format of the data being sent ? If so, then there are better ways of reading the message

See Serial input basics - updated