简体   繁体   English

将char *数组传递给另一个函数,C中的指针麻烦

[英]Passing char * array to another function, Pointer trouble in c

Hey i am having trouble getting the proper value stored into the CHANNELS char array. 嘿,我无法将正确的值存储到CHANNELS char数组中。 I really think it could be a simple problem with my pointers. 我真的认为这可能是一个简单的问题。 I "think" i understand that when you declare char *CHANNELS[6] you are creating an array of pointers. 我“认为”我了解,当您声明char * CHANNELS [6]时,您正在创建一个指针数组。 so i pass through in my switch statement case 1 CHANNELS in the third argument i am not getting the proper value back. 所以我在第三个参数中的switch语句案例1 CHANNELS中通过,我没有得到正确的值。 Any help is great! 任何帮助都很棒! Some back ground: I am reading in 6 "channels" that each have 6 binary values. 一些背景知识:我正在读取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;

you have passed as 你已经通过

binEnter(&channel0, CHANNELS,0);

The prototype of bitEnter is void binEnter(void *channel, char *CHANNELS[6], int i) bitEnter的原型为void binEnter(void *channel, char *CHANNELS[6], int i)

But then you do: scanf("%s",(UCHAR *)channel); 但随后您要做: scanf("%s",(UCHAR *)channel);

  1. Why do you pass it as void * if you are going to process it as a string? 如果要将其作为字符串处理,为什么将其传递为void *
  2. You are trying to input a string, where the type is of UCHAR (which i assume to be an unsigned character). 您正在尝试输入一个字符串,其类型为UCHAR (我认为这是一个无符号字符)。 Doing this will overwrite the memory starting from the base of the address stored in channel , and depending on how the memory was allocated to the automatic variables they will be overwritten, an undefined behaviour. 这样做将从存储在channel中的地址的基数开始覆盖内存,并且取决于将内存分配给自动变量的方式,它们将被覆盖,这是不确定的行为。

Try dynamically allocating the strings which you input in the bitEnter function and then add it to the CHANNELS ? 尝试动态分配您在bitEnter函数中输入的字符串,然后将其添加到CHANNELS吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM