Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:34:02+00:00 2026-05-31T21:34:02+00:00

I have an Arduino that is processing a string by splitting it into an

  • 0

I have an Arduino that is processing a string by splitting it into an array. For some reason, however, after the processing function returns, the array can only be accessed once before it appears that the values become corrupted. In other words, I can access any element of the array, but when I do, I am unable to access any other elements of the array.

void loop(){
  int pin;
  Serial.print("Enter command: ");
  while(Serial.available()<=0)
    delay(100);
///Input to the serial terminal was: "This;is;a;command". Notice how inside the getCommands() function, it will output all elements ok

  char** commands = getCommands();
  Serial.println(commands[1]); ///prints "is"
  Serial.println(commands[0]); ///**** prints nothing, or sometimes infinite spaces****
  delay(1000);
}
char** getCommands(){
  char* commandIn = getSerialString();
  char* commands[10];
  char *str;
 int i=0;
 while ((str = strtok_r(commandIn, ";", &commandIn)) != NULL){

   commands[i]=str;
   i++;
}
Serial.println(commands[0]);   ///prints "This"
Serial.println(commands[1]);   ///prints "is"
Serial.println(commands[2]);   ///prints "a"

return commands;
}
char* getSerialString(){
  while(Serial.available()<=0)
    delay(100);
  int i=0;
  char commandbuffer[100];
  for(int a=0; a<100; a++)
    commandbuffer[a]='\0';

  if(Serial.available()){
     delay(100);
     while( Serial.available() && i< 99) {
        commandbuffer[i++] = Serial.read();
     }
     commandbuffer[i++]='\0';
  }
  return commandbuffer;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:34:03+00:00Added an answer on May 31, 2026 at 9:34 pm
    char** getCommands(){
      char* commands[10];
      …
      return commands;
    }
    

    The statement return commands doesn’t return the value of the array, it returns the address of the array. Technically, the type of the expression commands decays from array-10-of-pointer-to-char to pointer-to-pointer-to-char in this context; the value of the expression is the address of the first element of the array.

    As a result, you return the address of a local variable, which local variable ceases to exist after the return statement. Later, in loop, you dereference this pointer to a destroyed object, resulting in undefined behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Arduino sketch that spans several files. There is a function
I have some code I have in the Arduino environment that requires x (in
I have the following code on my Arduino that constantly checks for a serial
I have few projects ideas that involve plugging a computer or an arduino to
I have a Arduino app that needs to talk to my PC across the
I have a Python script that writes data packets to an Arduino board through
I am fetching data from the following database: I have arduino-box that send that
I have an Arduino sketch that takes a timet and when that timet is
I have an Arduino sketch that takes a timet and when that timet is
I have an Arduino application (well actually a library) that has a number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.