[英]Passing char * array to another function, Pointer trouble in c
嘿,我无法将正确的值存储到CHANNELS char数组中。 我真的认为这可能是一个简单的问题。 我“认为”我了解,当您声明char * CHANNELS [6]时,您正在创建一个指针数组。 所以我在第三个参数中的switch语句案例1 CHANNELS中通过,我没有得到正确的值。 任何帮助都很棒! 一些背景知识:我正在读取6个“通道”,每个通道都有6个二进制值。
void binEnter(void *channel, char *CHANNELS[6], int i){
redo:
printf("Enter binary value for Channel %d: ",i);
scanf("%s",(UCHAR *)channel);
if (strlen(channel)!=6) {
printf("Error entry must be six digits!\n");
goto redo;
}
char *string = channel;
int j;
for (j = 0; j < 6; j++){
if ((string[j] != '0') && (string[j] != '1')){
printf("Error did not enter a binary number!\n");
goto redo;
}
}
CHANNELS[i]=channel;
printf("Channel %d is stored as %s\n",i,CHANNELS[i]);
}
int main(){
int selection;
UCHAR channel0;
UCHAR channel1;
UCHAR channel2;
UCHAR channel3;
UCHAR channel4;
UCHAR channel5;
char *CHANNELS[6];
float vRefVal[6];
//char *data[5];
float volt =0;
do {
start:
promptUser();
scanf("%d",&selection);
switch (selection) {
case 1:
binEnter(&channel0, CHANNELS,0);
binEnter(&channel1, CHANNELS,1);
binEnter(&channel2, CHANNELS,2);
binEnter(&channel3, CHANNELS,3);
binEnter(&channel4, CHANNELS,4);
binEnter(&channel5, CHANNELS,5);
int i;
for (i=0; i<6; i++) {
printf("Channel %d is %s in main\n", i, CHANNELS[i]);
}
goto start;
case 2:
goto start;
case 3:
enterVolt(&volt);
printf("Volt = %f\n",volt);
goto start;
case 4:
if (volt) {
vRefCal(&volt, CHANNELS, vRefVal);
printVref(vRefVal);
goto start;
}
else{
printf("Must enter input Vref first!\n");
goto start;
}
default:
break;
}
} while (selection!=5);
return 1;
}
UCHAR channel0;
你已经通过
binEnter(&channel0, CHANNELS,0);
bitEnter
的原型为void binEnter(void *channel, char *CHANNELS[6], int i)
但随后您要做: scanf("%s",(UCHAR *)channel);
void *
? UCHAR
(我认为这是一个无符号字符)。 这样做将从存储在channel
中的地址的基数开始覆盖内存,并且取决于将内存分配给自动变量的方式,它们将被覆盖,这是不确定的行为。 尝试动态分配您在bitEnter
函数中输入的字符串,然后将其添加到CHANNELS
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.