Error: value computed is not used [-Werror=unused-value]
Error: value computed is not used [-Werror=unused-value]
我写了这段代码来尝试读取单词中是否有重复的字母,但我一直遇到这个错误:
错误:未使用计算的值 [-Werror=unused-value]
有问题的行是:
arr[(int)(str[i])++];
整个代码为:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int arr[256]={0};
for(i=0;i<strlen(str);i++)
{
if(str[i]==' '){
continue;
arr[(int)(str[i])++];
}
}
printf("Repeated character in a string are:\n");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
printf("%c occurs %d times\n",(char)(i),arr[i]);
}}
return 0;
}
这是来自控制台的错误消息:
https://i.stack.imgur.com/GpMOW.png
感谢任何帮助:)
我对代码做了一些修改,如注释所示。
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%29[^\n]", str); // limit the input length
int i;
int arr[256] = { 0 };
for(i = 0; i < strlen(str); i++)
{
if(str[i] == ' '){
continue;
}
// this line was repositioned so that it can execute
// char can be signed, so don't index by a negative value
// the post-increment should apply to the `arr[]` array element
arr[ (unsigned char)str[i] ]++;
}
printf("Repeated character in a string are:\n");
for(i = 0; i < 256; i++)
{
if(arr[i] > 0) // inform *any* usage
{
// (char)i gets promoted to int anyway
printf("%c occurs %d times\n", i, arr[i]);
}
}
return 0;
}
会话:
Enter your String:one two
Repeated character in a string are:
e occurs 1 times
n occurs 1 times
o occurs 2 times
t occurs 1 times
w occurs 1 times
我写了这段代码来尝试读取单词中是否有重复的字母,但我一直遇到这个错误:
错误:未使用计算的值 [-Werror=unused-value]
有问题的行是:
arr[(int)(str[i])++];
整个代码为:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int arr[256]={0};
for(i=0;i<strlen(str);i++)
{
if(str[i]==' '){
continue;
arr[(int)(str[i])++];
}
}
printf("Repeated character in a string are:\n");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
printf("%c occurs %d times\n",(char)(i),arr[i]);
}}
return 0;
}
这是来自控制台的错误消息: https://i.stack.imgur.com/GpMOW.png
感谢任何帮助:)
我对代码做了一些修改,如注释所示。
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%29[^\n]", str); // limit the input length
int i;
int arr[256] = { 0 };
for(i = 0; i < strlen(str); i++)
{
if(str[i] == ' '){
continue;
}
// this line was repositioned so that it can execute
// char can be signed, so don't index by a negative value
// the post-increment should apply to the `arr[]` array element
arr[ (unsigned char)str[i] ]++;
}
printf("Repeated character in a string are:\n");
for(i = 0; i < 256; i++)
{
if(arr[i] > 0) // inform *any* usage
{
// (char)i gets promoted to int anyway
printf("%c occurs %d times\n", i, arr[i]);
}
}
return 0;
}
会话:
Enter your String:one two
Repeated character in a string are:
e occurs 1 times
n occurs 1 times
o occurs 2 times
t occurs 1 times
w occurs 1 times