user53790: well I am trying to get array size  to convert hex value to int value. but if I try to get size of array it returns every time 2. Actually .I dont understand. how to get array size, can you help me?
this is my code
unsigned long w;

void setup() {
  Serial.begin(9600);

  uint8_t * packet = read_packet();
  Serial.println(sizeof(packet)); // PROMLEM IS HERE, IT RETURNS EVERYTIME "2"

  for (int i = 0; i < 4; i++)
  {
    Serial.println(packet[i]); //if i print the packet array all elements are here
  }

  /*
    for (byte i = 0; i < sizeof packet; i++) {
      w = w + ((long)(packet[sizeof packet - (i + 1)]) << (i * 8));
    }

  */
}

void loop() {
  // put your main code here, to run repeatedly:

}

uint8_t *read_packet() {
  static uint8_t buffer[4];

  buffer[0] = 0x00;
  buffer[1] = 0x32;
  buffer[2] = 0x32;
  buffer[3] = 0x32;

  Serial.println(sizeof buffer);
  return buffer;
}

if I print the packet array, all 4 elements are there 0,50,50,50. but size of array returns every time "2". even if I increase the array size.
maybe you can think why am I using pointers? because of my previous question answer is using this method. Read serial with header and end marker

user37622: Majenko's answer explains things and shows how to fix your code, but the solution with static buffer inside the function is not common.
Common is to have int readBuffer(unit8_t* buffer, size_t size).
The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.
Real example:
int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}


user4143: A pointer is just a pointer. It's not an array. On 8 bit Arduinos the memory address range is a 16 bit value, so a pointer will always be 2 bytes.
You need to return two values from your function - the buffer pointer and the length of the buffer. This is usually done by passing an extra integer pointer parameter:
unsigned long w;

void setup() {
  Serial.begin(9600);

  uint8_t plen;
  uint8_t * packet = read_packet(&plen); // Pass the pointer to the plen variable to store the length in

  Serial.println(plen); // This now prints "4"

  for (int i = 0; i < plen; i++) // You can now use `plen` here
  {
    Serial.println(packet[i]); //if i print the packet array all elements are here
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

uint8_t *read_packet(uint8_t *len) {

  static uint8_t buffer[4];

  buffer[0] = 0x00;
  buffer[1] = 0x32;
  buffer[2] = 0x32;
  buffer[3] = 0x32;

  if (len != NULL) *len = 4; // Store 4 in the length variable if provided

  return buffer;
}


53790: <p>well I am trying to get array size  to convert hex value to int value. but if I try to get size of array it returns every time 2. Actually .I dont understand. how to get array size, can you help me?</p>
<p>this is my code</p>
<pre><code>unsigned long w;

void setup() {
  Serial.begin(9600);

  uint8_t * packet = read_packet();
  Serial.println(sizeof(packet)); // PROMLEM IS HERE, IT RETURNS EVERYTIME &quot;2&quot;

  for (int i = 0; i &lt; 4; i++)
  {
    Serial.println(packet[i]); //if i print the packet array all elements are here
  }

  /*
    for (byte i = 0; i &lt; sizeof packet; i++) {
      w = w + ((long)(packet[sizeof packet - (i + 1)]) &lt;&lt; (i * 8));
    }

  */
}

void loop() {
  // put your main code here, to run repeatedly:

}

uint8_t *read_packet() {
  static uint8_t buffer[4];

  buffer[0] = 0x00;
  buffer[1] = 0x32;
  buffer[2] = 0x32;
  buffer[3] = 0x32;

  Serial.println(sizeof buffer);
  return buffer;
}
</code></pre>
<p>if I print the packet array, all 4 elements are there 0,50,50,50. but size of array returns every time &quot;2&quot;. even if I increase the array size.</p>
<p>maybe you can think why am I using pointers? because of my previous question answer is using this method. <a href="https://arduino.stackexchange.com/questions/85326/read-serial-with-header-and-end-marker/85328?noredirect=1#comment194331_85328">Read serial with header and end marker</a></p>
 37622: <p>Majenko's answer explains things and shows how to fix your code, but the solution with static buffer inside the function is not common.</p>
<p>Common is to have <code>int readBuffer(unit8_t* buffer, size_t size)</code>.</p>
<p>The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.</p>
<p>Real example:</p>
<pre><code>int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 &amp;&amp; size &gt; rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i &lt; l &amp;&amp; i &lt; size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size &lt;= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}
</code></pre>
 4143: <p>A pointer is just a pointer. It's not an array. On 8 bit Arduinos the memory address range is a 16 bit value, so a pointer will always be 2 bytes.</p>
<p>You need to return two values from your function - the buffer pointer <em>and</em> the length of the buffer. This is usually done by passing an extra integer pointer parameter:</p>
<pre class="lang-cpp prettyprint-override"><code>unsigned long w;

void setup() {
  Serial.begin(9600);

  uint8_t plen;
  uint8_t * packet = read_packet(&amp;plen); // Pass the pointer to the plen variable to store the length in

  Serial.println(plen); // This now prints &quot;4&quot;

  for (int i = 0; i &lt; plen; i++) // You can now use `plen` here
  {
    Serial.println(packet[i]); //if i print the packet array all elements are here
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

uint8_t *read_packet(uint8_t *len) {

  static uint8_t buffer[4];

  buffer[0] = 0x00;
  buffer[1] = 0x32;
  buffer[2] = 0x32;
  buffer[3] = 0x32;

  if (len != NULL) *len = 4; // Store 4 in the length variable if provided

  return buffer;
}
</code></pre>
