Forum: Compiler & IDEs Frage zu Pointern


von Sebastian L. (sebastian_l)


Lesenswert?

Hallo zusammen,

ich habe eine Frage zu Pointer (stehe mit denen echt auf Kriegsfuss ;-( 
). Ist zwar ein Fujitsu 16fx / 96340 Projekt mit Softune, denke aber, 
dass es sich eher um eine grundsätzliche Frage handelt.

Also - in meinem Hauptprogramm ist folgendes drin:
1
char webIfParsed[64];
2
...
3
*webIfParsed = 0;
4
parse_command(webIfGotData, webIfParsed);
5
if (strcmp(webIfParsed, "getstatus") == 0)  // status requested
6
{
7
  ....
8
}

Klappt soweit wunderbar, der Vergleich funktioniert und die If-Bedingung 
wird auch wie gewünscht ausgeführt.

Nun zum Problem - ich möchte gerne innerhalb von parse_command bereits 
einen Vergleich auf den Wert machen:
1
void parse_command(const char *input, char* command)
2
{
3
  // Achtung, hier nicht webIfParsed, sondern command
4
  /* copy first token to command */
5
  while (( *input != 0) && (*input != ' ') && (*input != 10))
6
  {
7
    *command = *input;
8
    command++;
9
    input++;
10
  }
11
  *command = 0;
12
  
13
  // HIER DER VERGLEICH
14
  if (strcmp(command, "getstatus") == 0)
15
  {
16
    ....
17
  }
18
....
19
}

Dieser Vergleich klappt nicht ?! Ich stehe echt auf dem Schlauch, kann 
mir jemand helfen?

Danke,
Sebastian

von Peter II (Gast)


Lesenswert?

Sebastian L. schrieb:
> *command = 0;
>   // HIER DER VERGLEICH
>   if (strcmp(command, "getstatus") == 0)


damit wird strcmp immer feststellen das command leer ist.

von DirkB (Gast)


Lesenswert?

Durch das command++; zeigt command ja auch nicht mehr auf den Anfang von 
webIfParsed.
Du must dir den Zeiger auf den Anfang merken.

von Sebastian L. (sebastian_l)


Lesenswert?

Hallo ihr zwei,

genial - 100000 Dank ! (ohne Übertreibung ;-). Ich sag ja, ich steh mit 
Pointern auf Kriegsfuss.

Merke mir jetzt mit einer weiteren Variable den Anfang und vergleiche 
dagegen, hier nochmal komplett, wenn jemand mal eine entsprechende 
Lösung sucht ;-)
1
void parse_command(const char *input, char* command)
2
{
3
  char* ptr_val;
4
5
  // Achtung, hier nicht webIfParsed, sondern command
6
  /* copy first token to command */
7
  ptr_val = command;
8
  while (( *input != 0) && (*input != ' ') && (*input != 10))
9
  {
10
    *command = *input;
11
    command++;
12
    input++;
13
  }
14
  *command = 0;
15
  
16
  // HIER DER VERGLEICH
17
  if (strcmp(ptr_val, "getstatus") == 0)
18
  {
19
    ....
20
  }
21
....
22
}

von DirkB (Gast)


Lesenswert?

Sebastian L. schrieb:
> (*input != 10)

Wenn du mit der 10 '\n' meinst, dann schreib auch '\n': (*input != 
'\n')

von Sebastian L. (sebastian_l)


Lesenswert?

Hallo,

oh ja, Danke !

Schon korrigiert ;-)
Wie logisch doch immer alles im Nachhinein ist ;-)


Sebastian

von Karl H. (kbuchegg)


Lesenswert?

1
  while (( *input != 0) && (*input != ' ') && (*input != 10))

fehlt nur noch der dritte im Bunde der Whitespace Zeichen: der Tabulator
1
while ((*input != '\0') && (*input != ' ') &&
2
       (*input != '\t') && (*input != '\n'))

von Rolf M. (rmagnus)


Lesenswert?

Karl Heinz Buchegger schrieb:
> fehlt nur noch der dritte im Bunde der Whitespace Zeichen: der Tabulator
>
> while ((*input != '\0') && (*input != ' ') &&
>        (*input != '\t') && (*input != '\n'))
1
while (*input != '\0' && !isspace(*input))

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.