pointer and strings

Hi,

I have a weird problem.

Using this code:

uint8_t cnt=0;
char inChar;
char string[20]="hallo;
char *_string;

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


void loop(void){
  while(Serial.available() > 0){
    inChar = Serial.read();
    string[cnt] = inChar;
    cnt++;
    string[cnt] = '\0';
  }
  
  _string = string;
  
  for(; *_string!='\0' ; _string++){
    Serial.println(*_string);
  }
  
  string[0]='\0';

It puts just out "hello", but if I make a input it's print just the first letter of the array.
But if I use instead *_string as an array with a fieldmarker (_string[bla]) I can read out
the complete input getting from the serial cache.

What is the difference between the char array I'd allocate at startup and that ones, that's read out over serial?

Many thanks for your help

_string is already a pointer, so you don't need to dereference it.

You wouldn't say println(*string), right? I am not sure where you stored your array, but it wasn't in string.

I don't understand your question. And I don't think that code compiles; it's missing at least the closing quote on the string "hello.

Could you try again with a little more detail about what you expected and what you got instead?

And could you post your real code, all of it?

Thanks,

-br

char inChar;
char string[20]="hallo;
char *_string;

And that compiles?

No, it don't. I'd copied it bad.

I mean, there is a difference between the string I set up on top and that one I create out of the serial input.
Serial.println(*_string) is used, because I want to print every field of the array separately.

uint8_t cnt=0;
char inChar;
char string[20]="hallo";
char *_string;

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


void loop(void){
  while(Serial.available() > 0){
    inChar = Serial.read();
    string[cnt] = inChar;
    cnt++;
    string[cnt] = '\0';
  }
  
  cnt=0;
  _string = string;
  
  for(; *_string!='\0' ; _string++){
    Serial.println(*_string);
  }
  
  string[0]='\0';
}

After setting cnt=0 it does what I want. But don't really know why.

The two strings handles even different. For example using the same
code, but without dereferencing _string in Serial.println it print out that:

hallo
allo
llo
lo
o

what is pretty logical, but if I make a input over serial, it prints that (same word):

h
a
l
l
o

Please excuse my bad english, maybe somebody get the meaning right.

I think the problem is you are assuming that when you hit enter in the serial monitor, Serial.available() instantly returns however many bytes you sent it. Serial communication is slow compared to how quickly your loop() runs. loop() will run hundreds to thousands of times between when it receives a byte, so it's unlikely that Serial.available() will ever return anything greater than 1. To combat that, you need a terminating character that tells your code "ok, i'm done sending Serial information for now" such as a new line or carriage return. When you receive that, you can start doing things with the full string.

Here is an example I posted eariler:

thanks for your explanation.
one more question:

void setup(void){Serial.begin(9600);}
  
char *p_string(void){
  char string[20];
  uint8_t cnt=0;
  char inChar;

  if(Serial.available()){
    while(inChar!='\r'){
      if(Serial.available()){
        inChar=Serial.read();
        string[cnt]=inChar;
        cnt++;
        string[cnt] = '\0';
      }
    }
    Serial.println(string);
    return string;
  }

}
    
void loop(void){
  char *a=p_string();
  if(a) Serial.println(a);
}

why do the first println() prints the whole input, but the second one in the loop function just the first four chars? ever char behind is rubbish.

char *p_string(void){
  char string[20];
...
   return string;
  }

}

Never do this.
When you come to use the pointer returned, the entity it points to no longer exists.

hehe.. right. should had find the answer by my own.

but, actually i used a e-book for exemplification. there is used following code for example:

#include <stdio.h>
#include <string.h>
#define MAX 255

char *eingabe(char *str)
{
   char input[MAX];
   printf("Bitte \"%s\" eingeben: ",str);
   fgets(input, MAX, stdin);
   return strtok(input, "\n");
}

int main()
{
   char *ptr;
   ptr = eingabe("Vorname");
   printf("Hallo %s\n",ptr);

   ptr = eingabe("Nachname");
   printf("%s, interssanter Nachname\n",ptr);
   return 0;
}

i tried it out. g++ compiled it and no chars gone lost. what's the difference between that
one and for example that one:

char* p_string(void){
        char str[]="basdjakdjsak";
        printf("%s\n", str);
        return str;
}

both returned pointer lost it's target

strtok has its own, private static buffer, so it is quite legal to return a pointer to it.

In the other case, the function is returning a pointer to an automatic variable allocated on the stack which will go out of scope as soon as the function returns.