Doubt in programming

Hi guys,

I'm starting with c (arduino) programming. I would like to know why this simple program below does not work! Why not print on the serial? Can you help me.

Thanks.

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

void loop() {
  
  char* teste = criaVetor();
  Serial.print("Resultado: ");
  Serial.println(teste);
  
}

char* criaVetor(){
   char vetor[10]="Olá mundo!";  
   return vetor;
   
}

Try this

char vetor[]="Olá mundo!";  // is now a global char array with full scope, whole sketch can address
                     // the length is 11 bytes set by the initializing text, 10 letters plus required terminating char == 0.  


void setup() // in here go things to only run once
{
  Serial.begin(115200);
  
  char* teste = vetor;  // the name vetor is a pointer to the 1st char of the char array vetor[]
  Serial.print("Resultado: ");
  Serial.println(teste);
 }

void loop() // you don't want to print that line over and over do you?
{
}

Thanks for the answer! But how do I do this using a function?

Add the static keyword before this declaration: char vetor = "Olá mundo!";

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

void loop() 
{
  char *teste  = criaVetor();
  Serial.print("Resultado: ");
  Serial.println(teste);
  delay(1000);
}

char *criaVetor() 
{
  static char vetor[] = "Olá mundo!";
  return vetor;
}

smk.png

Why has the keyword static solved the problem?

Static Variables in C. Static variables have a property of preserving their value even after they are out of their scope!

smk.png

oroski: Thanks for the answer! But how do I do this using a function?

Why? The compiler might optimize the function call out, if it doesn't then the function call only makes a slower, fatter sketch.

But if you like taking extra steps....

char vetor[]="Olá mundo!";  // is now a global char array with full scope, whole sketch can address
                     // the length is 11 bytes set by the initializing text, 10 letters plus required terminating char == 0.  

char* criaVetor()
{
   return vetor;  // the name vetor is a pointer to the 1st char of the char array vetor[]
}


void setup() // in here go things to only run once
{
  Serial.begin(115200);
  
  char* teste = criaVetor();  // the name vetor is a pointer to the 1st char of the char array vetor[]
  Serial.print("Resultado: ");
  Serial.println(teste);
 }

void loop() // you don't want to print that line over and over do you?
{
}
void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  char *teste  = criaVetor();
  Serial.print("Resultado: ");
  Serial.println(teste);
  while(1);//delay(1000);
}

char *criaVetor() 
{
  static char vetor[] = "Olá mundo!";
  return vetor;
}

smbz.png

smbz.png

oroski: Hi guys,

I'm starting with c (arduino) programming. I would like to know why this simple program below does not work! Why not print on the serial? Can you help me.

Thanks.

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

void loop() {     char* teste = criaVetor();   Serial.print("Resultado: ");   Serial.println(teste);   }

char* criaVetor(){   char vetor[10]="Olá mundo!";    return vetor;  
}

The reason it doesn't work is that vetor is local to the function criaVetor() . When the code leaves the function, vetor ceases to exist, and the memory it is stored in is subject to reuse by other parts of the code. Reply #3 gives one possible solution, by making vetor static it will persist between calls to criaVetor, but vetor is still local to the function it is created in, so cannot be referenced by the variable name outside of that function. A common technique would be to create a char array either as a global variable, or inside the function that calls criaVetor, and pass a pointer to that array to criaVetor, so that it can place the text inside the array. (Note that the text will not fit within a 10 char array, the á character occupies more than 1 char, the compiler should have been giving you a warning message about this).

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

void loop() {
  char teste[12];
  criaVetor(teste);
  Serial.print("Resultado: ");
  Serial.println(teste);

}

void criaVetor(char * vetor) {
  strcpy(vetor, "Olá mundo!");
}

Variables declared in a function are not directly visible outside of the function. They are "local to the function".

If they are declared static they keep their values and if they are not static they disappear when the function exits.

Static local variables are kept on the heap in RAM where they stay, non-static local variables are kept on the stack and let go when the function returns, the next function may overwrite whatever was on the stack.

Normally we use local variables to keep code from outside of the function from changing them. We can have several functions using the same name(s) for local variables.

So having a static local variable is not a bad thing but when the function passes the address to it, whatever gets the address can access the variable and defeat the purpose of having a local variable. Sure, you can do it and think it makes you special-different but what kind of special? You either didn't learn what something is for or you like to jerk people around by playing mysterious as if you know some kind of "Leet" understanding that's somehow better because it's twisted.

Normally we put the global variable declarations up at the top of the code so that other people and ourselves can find them easily. Packing them into functions that give the address away means looking deeper for harder to use code.

oroski: Thanks for the answer! But how do I do this using a function?

david_2018: The reason it doesn't work is that vetor is local to the function criaVetor() . When the code leaves the function, vetor ceases to exist, and the memory it is stored in is subject to reuse by other parts of the code. Reply #3 gives one possible solution, by making vetor static it will persist between calls to criaVetor, but vetor is still local to the function it is created in, so cannot be referenced by the variable name outside of that function. A common technique would be to create a char array either as a global variable, or inside the function that calls criaVetor, and pass a pointer to that array to criaVetor, so that it can place the text inside the array. (Note that the text will not fit within a 10 char array, the á character occupies more than 1 char, the compiler should have been giving you a warning message about this).

But, the OP has exclusively wanted that the criaVetor() function will not take any parameter; rather, it will return a parameter. It could be an exercise for him.

GolamMostafa: But, the OP has exclusively wanted that the criaVetor() function will not take any parameter; rather, it will return a parameter. t could be an exercise for him.

Ok, I missed the requirements for the function.

Tks!!!

You guys helped me a lot.

If the function returns the address of a global string it is still a function.

The function could return the contents of the string instead of the address of the string to protect the string. But then declaring the string as const protects it at the compiler level.

AVR CPU is a Reduced Instruction Set machine. The chip runs simple instructions at nearly the clock rate, the simpler your code is the faster it should run with the exceptions of blocked code like during delay().

oroski: Hi guys,

I'm starting with c (arduino) programming. I would like to know why this simple program below does not work! Why not print on the serial? Can you help me.

Thanks.

Do you have a favorite C language tutorial site? Your English is good but are there any in your native language, easier to catch nuances? If not then perhaps there is need?

The original C book is not very thick and the web is full of examples.