C-数字不输入"if condition",不知道为什么

  • 本文关键字:不知道 condition if 数字 c
  • 更新时间 :
  • 英文 :


所以,基本上,我被指示制作一个函数,询问用户的大小,然后用用户选择的元素创建一个数组。。。例如:大小:4,输入:10 20 30 40,创建的数组={10,20,30,40}。然后,用户的下一步是为这个创建的数组应用一些函数。示例:

例如,如果用户选择字母"A";add1";将被应用,并且阵列的所有元素将在一个单元中增加,因此对于{10,20,30,40}的输入示例,输出将为{11,21,31,41}。

我的代码不起作用,为什么?有人能帮我吗?我已经使用了调试器,并且函数没有进入";如果条件";。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* add1(int* array, int size)
{
int *novo;
novo = malloc(size*sizeof(int));
for (int i = 0; i < size; i++){
*(novo + (i)) = *(array + i)+1;
}
return novo;
};

int* add2(int* array, int size)
{
int *novo;
novo = malloc(size*sizeof(int));
for (int i = 0; i < size; i++){
*(novo + (i)) = *(array + i)+2;
}
return novo;
};

void print1(int* array, int size)
{
for (int i = 0; i < size; i++)
{
printf("%in", *(array+i));
}
};


int main(void)
{   int i, elemento, size;
char *new;
printf("Insert Size:n");
scanf("%i", &size);
int newArray[size];
printf("Insert Elements:n");
for (i = 0; i < size; i++)
{
scanf("%i", &elemento);
newArray[i] = elemento;
}
printf("Select option:n");
scanf(" %c", &new);
if (new == 'A') {
int* result;
result = add1(&newArray, size);
print1(result, size);
} else if (new == 'B') {
int* result;
result = add2(&newArray, size);
print1(result, size);
} else if (new == 'C') {
} else if (new == 'D') {
}
}
  1. 更改:

    char *new;
    

char new;

作为

scanf(" %c", &new);

正在期待一个字符,而就代码而言,new是一个未定义的字符指针。因此,将指针传递给未定义的指针是不好的。

请打开你的编译器警告,这会被发现!

  1. new不是一个好的变量名。由于它导致与C++关键字混淆

  2. 检查scanf的返回值-请参阅该的手册页面

  3. 也许使用switch而不是if new == 'A' .....

最新更新