Hey i am having trouble getting the proper value stored into the CHANNELS char array. 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. so i pass through in my switch statement case 1 CHANNELS in the third argument i am not getting the proper value back. Any help is great! Some back ground: I am reading in 6 "channels" that each have 6 binary values.
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)
But then you do: scanf("%s",(UCHAR *)channel);
void *
if you are going to process it as a string? UCHAR
(which i assume to be an unsigned character). 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. Try dynamically allocating the strings which you input in the bitEnter
function and then add it to the CHANNELS
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.