有关'字符串'之间转换的奇怪警告和' int'

时间:2018-03-14 18:43:11

标签: c list sequential

我顺便使用CLion。

int ch;
printf("Insert the operation you want to execute: ");
scanf("%d", &ch); //HERE

我将ch声明为整数变量,并使用 scanf %d ,我希望用户插入一个值以插入ch变量,但这里有一个警告弹出起来说:" ' scanf的'用于将字符串转换为整数值"

我无法理解为什么......

以下是评论中要求的整个代码: (代码被修改,因为我从昨天起改变了它)

typedef unsigned int boolean;

struct list{
float * buffer;
int size;
int head;
int tail;
};

int getsize();
float getvalue();
void init(struct list*, int);
boolean suf_insert(struct list*, float, int);
boolean pre_insert(struct list*, float, int);
void visit(struct list*);

int main(){

struct list listA;
struct list listB;
int size=0;

int ch;

while(1){

    printf("Sequential Lists operations\n");
    printf("1.  Insert the size of the array\n");
    printf("2.  Initialize the list A\n");
    printf("3.  Initialize the list B\n");
    printf("4.  \n");
    printf("5.  \n");
    printf("6.  \n");
    printf("7.  \n");

    printf("\nInsert the operation you want to execute: ");
    scanf("%d", &ch);
    switch(ch){
        case 1: size=getsize();
            break;
        case 2: init(&listA, size);
            break;
        case 3: init(&listB, size);
            break;
        case 4: getvalue();
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;

        default: printf("\nInvalid command. Retry\n");
            break;
    }
}

}
int getsize(){
int size;
printf("Insert the size of the list you want to create: ");
scanf("%d", &size);
return size;
}

float getvalue(){
float value;
printf("The value you want to insert: ");
scanf("%f", &value);

return value;
}

void init(struct list * ptr, int size) {

if (size != 0) {
    ptr->buffer = (float *) malloc(size * sizeof(int));
    ptr->size = size;
    ptr->head = 0;
    ptr->tail = 0;
} else {
    printf("\nBefore continue, insert the size.\n");
}
}

boolean suf_insert(struct list * ptr, float value, int size){

    if(((ptr->tail + 1) % ptr->size) != ptr->head && size!=0) {

        ptr->buffer[ptr->tail] = value;
        ptr->tail = (ptr->tail + 1) % ptr->size;

        return TRUE;
    } else {

        return FALSE;
    }
}

boolean pre_insert(struct list * ptr, float value, int size){

if(((ptr->tail + 1) % ptr->size) != ptr->head && size!=0){

        ptr->head = (ptr->head + ptr->size - 1) % ptr->size;
        ptr->buffer[ptr->head] = value;

        return TRUE;
}else{

        return FALSE;
}
}

void visit(struct list * ptr){

int position;
for(position=ptr->head; position!=ptr->tail; position=(position+1)%ptr-     >size){
    printf("%f", ptr->buffer[position]);
}

}

它看起来像我必须重复:这段代码ISN完美,可能恰恰相反!我还在努力!

1 个答案:

答案 0 :(得分:3)

警告很清楚为什么;你省略了“功能不会报告转换错误”的部分。然而,这显然也是无稽之谈:strtol()也不会报告转换错误 - 如果存在转换错误,它只返回零 - 但是与成功转换为“0”无法区分。

scanf()至少返回值是成功或其他方面的明确指示。确实scanf()没有报告转化错误,它会报告成功转化的次数,所以当这种情况下只有一次转化时,返回值为零会报告转化错误。

“警告”是由您的IDE而不是您的编译器生成的,仅仅是建议(不良建议IMO) - 您可以明确选择忽略该建议。

但你至少应该检查来自scanf()的返回值。如果scanf()失败,则ch的值未定义。 strtol()的一个优点是,在失败时它至少返回一个定义的值 - 零 - 并且至少在特定的情况下,零是无效值,因此它具有优点;但不是出于警告中提出的原因。

相关问题