Returning a char array pointer from a function

0

Good day

  1. The problem:

    • I am trying to create functions that return a char array or a pointer to the array.
    • In the past I used String arrays and String functions, and would like to get a better control of my memory allocation.
    • Please see code below. If this is completely the wrong approach please advise on direction.
  2. Code:

char cBuffer[255];

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

void loop() {
  char *p = function();
  strcpy(cBuffer,p);
  Serial.println(cBuffer);
}

char *function(){
  char cTemp[] = "some data";
  char *p = cTemp;
  return p;
}
  1. Error Msg:
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400013f9  PS      : 0x00060130  A0      : 0x800d0c27  A1      : 0x3ffb1f70  
A2      : 0x3ffc00b4  A3      : 0x00000000  A4      : 0x000000ff  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x3ffb1f30  
A10     : 0x3ffc00b4  A11     : 0x0000000b  A12     : 0x0000000c  A13     : 0x3ffb1f5c  
A14     : 0x0063787a  A15     : 0x72657771  SAR     : 0x0000001f  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400013f9  LEND    : 0x4000140d  LCOUNT  : 0xffffffff  

Backtrace: 0x400013f9:0x3ffb1f70 0x400d0c24:0x3ffb1f80 0x400d1c1f:0x3ffb1fb0 0x40088215:0x3ffb1fd0

Any help would be greatly appreciated.

c
pointers
arduino
asked on Stack Overflow Jan 10, 2020 by Misha • edited Jan 10, 2020 by Misha

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0