Forum: Mikrocontroller und Digitale Elektronik String aus Stringtabelle Pointer zeigt in den Wald!?


von Jan H. (janiiix3)


Lesenswert?

Moin,

irgendwie habe ich heute ein Pointer Problem.
Wieso kann ich nicht den Inhalt mit "strcpy" kopieren?
Was mache ich bei der Pointer Verarbeitung falsch?
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <string.h>
5
6
7
#define HC05T_EOC  "\r\n" // end of command string
8
9
10
11
typedef struct{
12
  char *cmd;
13
}hc05t_t;
14
15
/*
16
*  command table for the bluetooth µc
17
*   attention, for some one at commands you need the '=' character
18
*/
19
const hc05t_t hc05tCmd[] = 
20
{
21
  "AT"      , //[0] test uart connection
22
  "AT+RESET"    , //[1] reset device
23
  "AT+VERSION"  , //[2] query firmware version
24
  "AT+ORGL"    , //[3] restore settings to factory defaults
25
  "AT+ADDR"    , //[4] query device bluetooth address
26
  "AT+NAME"    , //[5] query/set device name
27
  "AT+RNAME"    , //[6] query remote bluetooth device´s name
28
  "AT+ROLE"    , //[7] query/set device role
29
  "AT+CLASS"    , //[8] query/set class of device CoD
30
  "AT+IAC"    , //[9] query/set inquire access code
31
  "AT+INQM"    , //[10] query/set inquire access mode
32
  "AT+PSWD"    , //[11] query/set pairing passkey
33
  "AT+UART"    , //[12] query/set uart parameter
34
  "AT+CMODE"    , //[13] query/set communication mode
35
  "AT+BIND"    , //[14] query/set binding bluetooth address
36
  "AT+POLAR"    , //[15] query/set led output polarity
37
  "AT+PIO"    , //[16] set/reset a user i/o pin
38
  "AT+MPIO"    , //[17] set/reset multiple user i/0 pin
39
  "AT+MPIO?"    , //[18] query user i/o pin
40
  "AT+IPSCAN"      , //[19] query/set scanning parameters
41
  "AT+SNIFF"    , //[20] query/set sniff energy savings parameters
42
  "AT+SENM"    , //[21] query/set security & encryption modes
43
  "AT+RMSAD"    , //[22] delete authenticated device from list
44
  "AT+FSAD"    , //[23] find device from authenticated device list
45
  "AT+ADCN"    , //[24] query total number of device from authenticated device list
46
  "AT+MRAD"    , //[25] query most recently used authenticated device
47
  "AT+STATE"    , //[26] query current state of the device
48
  "AT+INIT"    , //[27] initialize spp profile
49
  "AT+INQ"    , //[28] query nearby discoverable devices
50
  "AT+INQC"    , //[29] cancel search for discoverable devices 
51
  "AT+PAIR"    , //[30] device pairing
52
  "AT+LINK"    , //[31] connect to a remote device
53
  "AT+DISC"    , //[32] disconnect from a remote device
54
  "AT+ENSNIFF"  , //[33] enter energy saving mode
55
  "AT+EXSNIFF"  , //[34] exit energy saving mode
56
};
57
58
void hc05tBuildCmd(const hc05t_t *cmd, char *cmdPara)
59
{
60
  char sendBuff[40] ="";
61
  strcpy(sendBuff,(const char *)cmd);
62
  strcat(sendBuff,cmdPara);
63
  
64
  printf(sendBuff);
65
}
66
67
int main(int argc, char *argv[]){
68
  
69
  hc05tBuildCmd(&hc05tCmd[10],"TEST_PARA");
70
  
71
  return 0;
72
}

von Stefan F. (Gast)


Lesenswert?

Ich glaube es muss

> hc05tBuildCmd(hc05tCmd[10],"TEST_PARA");

heissen, ohne das & Zeichen. Weil das Array Zeiger auf Strings enthält. 
Mit dem & bekommst du aber die Adresse des Zeigers auf einen String.

von Markus F. (mfro)


Lesenswert?

Jan H. schrieb:
> strcpy(sendBuff,(const char *)cmd);

spätestens wenn man an unvermuteter Stelle plötzlich einen Cast braucht, 
sollte beim Programmierer die rote Warnlampe angehen.

cmd ist ein Zeiger auf ein Element eines Arrays von structs. Das Element 
enthält einen Zeiger auf einen String. Anstatt den da rauszuholen, 
übergibst Du die Adresse der struct.

So wär's richtig:
1
strcpy(sendBuff, cmd->cmd);

Und plötzlich braucht man auch keinen Cast mehr.

von Jan H. (janiiix3)


Lesenswert?

PEINLICH
Danke! Es ist Montag morgens. Habe es Mega verscheckt.
Danke!!!

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.