在C语言中如何使用三目运算符处理字符串

3
尝试使用三元运算符基于平均成绩将字符串分配给“remarks”变量。但是,“remark”未能被正确地分配。
#include <stdio.h>

int main(void) {
    int grade1, grade2, grade3;
    double average;
    char remarks[15];

    printf("Enter Grades of each subjects: \nEnglish: ");
    scanf("%d", &grade1);
    printf("Mathematics: ");
    scanf("%d", &grade2);
    printf("Programming: ");
    scanf("%d", &grade3);

    average = (grade1 + grade2 + grade3) / 3;

    remarks[15] = (average > 74) ? "Passed":"Failed";
    printf("\nAverage grade: %.2lf %s", average, remarks);

这个程序旨在获取3个数字(成绩)的平均值,然后根据结果声明是否通过。

程序正确获取输入,但只打印出平均值。

当我修改代码并像这样编写时,它可以正常工作。

printf("\nAverage grade: %.2lf %s", average, average > 74 ? "Passed" : "Failed");

我认为问题出在获取所有分数后重新分配变量remarks。

编辑:在第一个printf语句中添加了占位符%s。


1
数组不可赋值。要将字符串字面量复制到字符数组中,请使用strcpy()函数。确保目标数组足够大以容纳结果(在这种情况下是这样的)。 - Avi Berger
2
这与三元运算符无关,而是关于如何使用数组和复制字符串。首先编写remarks[15] =“Passed”;,然后从那里找出为什么那行代码毫无意义。 - Lundin
3个回答

2

两个问题:

  • You're attempting to assign a string to a single array element, remarks[15], instead of the entire array. Furthermore, since arrays in C are zero-based, the array indices range from 0 to 14 - attempting to write to remarks[15] will write to memory outside of the array.

  • You cannot use = to assign array contents, strings or otherwise. You must use the strcpy library function1, like strcpy( remarks, average > 74 ? "Passed" : "Failed" ). Or you'll have to assign individual array elements:

    remarks[0] = average > 74 ? 'P' : 'F';
    remarks[1] = 'a';
    remarks[2] = average > 74 ? 's' : 'i';
    remarks[3] = average > 74 ? 's' : 'l';
    remarks[4] = 'e';
    remarks[5] = 'd';
    remarks[6] = 0;  // terminate the string
    


  1. 或者使用strncpymemcpy或类似的函数。

2

使用

  const char* remarks;
  remarks = (average > 74) ? "Passed":"Failed";
  printf("\nAverage grade: %.2lf", average, remarks);

或者

char remarks[15];
strcpy( remarks, (average>74) ? "Passed" : "Failed" );
printf( "\nAverage grade: %.2lf", average, remarks);

它们是不同的。 在第一个案例中,remarks是一个指针,在第二个案例中,remarks是一个字符数组。
如果你想知道char指针和const组合之间的区别,可以看一下这篇文章。
请注意,这是C++特有的,因为C允许其中一些:
Godbolt链接:https://godbolt.org/z/rz6bh4qzP 可写指针指向可写位置。
char* a1;     // ok
char* a2 = nullptr; // ok
char* a3 = "Hello"; // error: ISO C++ forbids converting a string constant to 'char*' 
a1 = nullptr; // ok
a1 = "Hello"; // error: ISO C++ forbids converting a string constant to 'char*' 
a1[0] = 'x';  // ok

可写指向常量位置的指针

const char* b1; // ok
const char* b2 = nullptr;   // ok
const char* b3 = "Hello";   // ok
b1 = nullptr; // ok
b1 = "Hello"; // ok
b1[0] = 'x';  // error: assignment of read-only location

指向可写位置的常量指针

char * const c1; // error: uninitialized const
char * const c2 = nullptr;  // ok
char * const c3 = "Hello";  // error: ISO C++ forbids converting a string constant to 'char*'
c1 = nullptr;    // error: assignment of read-only variable
c1 = "Hello";    // error: assignment of read-only variable
c1[0] = 'x';     // ok

指向常量位置的常量指针

const char * const f1;      // error: uninitialized 'const f1'
const char * const f2 = nullptr; // ok
const char * const f3 = "Hello"; // ok
f1 = nullptr; // error: assignment of read-only variable
f1 = "Hello"; // error: assignment of read-only variable
f1[0] = 'x';  // error: assignment of read-only location

2
这个问题从来没有标记为C++。在你突然转换到C++并开始长时间的、与主题无关的讲解C++特定功能之前,这是一个很好的答案。例如,C和C++之间的一个微妙差别是,C允许使用const而不给它赋值。基本上,这使得C适用于各种硬件相关的编程,而不像C++。同样,nullptr只存在于C++中。在C中,char* a3 = "Hello";是可以的(虽然很愚蠢),但在C++中是不允许的。等等。我会回滚你最后的编辑,因为它在这里全部都是错误的/与主题无关的。 - Lundin

2
你正在将一个 const char * 赋值给一个 charremarks[15])。而且这个索引超出了边界,因为 C 数组索引从 0 开始,对于一个包含 15 个元素的数组,最高索引是 14
改为使用以下代码,它仅将备注存储为指向正确字符串字面量的指针(const char *)。
char *remarks = (average > 74) ? "Passed" : "Failed";

最好使用const char *remarks - hyde
将代码解释为C而不是C++,事实上OP正在将char *(而不是const char *)分配给char。由于历史原因,尝试修改字符串字面量会产生UB的事实与字符串字面量的数据类型无关。这也是为什么您建议的更改可能不会引发编译器警告,即使在实践中const char *remarks会是更好的选择。 - John Bollinger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接