如何在使用for循环和数组时更改为c代码
how to change to c code while using for loop and array
我使用c++创建了以下代码,但无法将其更改为c。我尝试使用c在末尾显示输出列表,但无法获得列表。我需要c语言代码来进行下面的编码,它是用c++编写的。请帮帮我。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int num;
int state;
int a,b,data[num];
string element[2];
cout<<"Enter amount data: ";
cin>>num;
for(a=0;a<num;a++){
cout<<"Enter state: ";
cin>>state;
if(state==3){
element[0]="A";
} else if(state==10){
element[1]="B";
}
}
cout<<"Elements are: ";
for(b=0;b<num;b++){
cout<<" "<<element[b];
}
}
我尝试使用数组,但无法获得列表输出
#include<stdio.h>
#include<string.h>
int main(){
int num;
int state;
int a,b,data[num];
char str[];
printf("Enter amount data: ");
//cin>>num;
scanf("%d",num);
for(a=0;a<num;a++){
printf("Enter state: ");
//cin>>state;
printf("%d",state);
if(state==3){
*str[0]="A";
} else if(state==10){
*str[1]="B";
}
}
printf("Elemets are: ");
for(b=0;b<num;b++){
puts(*str[b])
}
}
让我们从C++代码开始,因为它包含一个bug。
string element[2]; // Array with 2 string
cout<<"Enter amount data: ";
cin>>num; // Read a number
...
cout<<"Elements are: ";
for(b=0;b<num;b++){ // Use the number as limit
cout<<" "<<element[b]; // Print all string from 0 to number-1
}
因此,如果我给程序输入42,程序将访问element[0]
、element[1]
、element[2]
、element[3]
。。。。,element[41]
,但数组只有2元素,因此程序正在数组外部访问,即程序具有未定义的行为。
将一个格式错误的C++程序转换为C似乎毫无意义,但当然,它也可以转换为同样格式错误的C程序。
因此,对于C程序:
规则#1
将编译器设置为最高警告级别并修复所有警告
在C编程中,编译器发出的警告通常表明存在错误。因此,你必须认真对待警告并加以纠正。如果编译器只生成一个警告,甚至不要运行程序。确保你首先有一个干净的编译(即无警告编译(。
现在用";gcc-墙";给我:
main.cpp: In function 'main':
main.cpp:8:11: error: array size missing in 'str'
8 | char str[];
| ^~~
main.cpp:12:14: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
12 | scanf("%d",num);
| ~^ ~~~
| | |
| | int
| int *
main.cpp:19:14: error: invalid type argument of unary '*' (have 'int')
19 | *str[0]="A";
| ^~~~~~~
main.cpp:21:14: error: invalid type argument of unary '*' (have 'int')
21 | *str[1]="B";
| ^~~~~~~
main.cpp:26:15: error: invalid type argument of unary '*' (have 'int')
26 | puts(*str[b])
| ^~~~~~~
main.cpp:26:23: error: expected ';' before '}' token
26 | puts(*str[b])
| ^
| ;
27 | }
| ~
main.cpp:7:14: warning: unused variable 'data' [-Wunused-variable]
7 | int a,b,data[num];
| ^~~~
因此,您有2个警告(其中一个是实际错误(,但更糟糕的是,您还有5个错误。所有这些告诉你该怎么解决。
你的主要问题是字符串!尝试将C++类型string
转换为C类型字符串的方法是错误的。以下是对C.中字符串的一点解释
C中没有字符串类型!
所以
char str[]; // is NOT in any way compatible with C++ string element[2];
(这实际上是C中的一个错误。(
C字符串是char
数组的一种特殊用法。特殊用法是char数组中的一个元素必须具有值' '
(也称为NUL(。然后,该元素发出字符串结束的信号。
所以考虑一下:
char strA[2] = "A"; // strA[0] is 'A' and strA[1] is ' '
以这种方式,您已经使C字符串等于"0";A";。注意:字符串长度为1,但数组仍然需要两个字符。这是因为' '
需要一个额外的字符来发出END-OF-STRING信号。
因此,如果你想要一个有两个字符串的数组来容纳";A";或";B";,你需要做:
char str[2][2]; // instead of just char str[];
此外,您不能使用=
为C中的字符串赋值。您需要strcpy
("字符串副本"(。类似:
strcpy(str[0], "A"); // instead of str[0] = "A"
最后,要打印后面有换行符的字符串,只需使用:
puts(str[0]); // or puts(str[1]);
由于字符串长度将在编译后决定,因此将由OS 在运行时设置
/* ... */
char str[];
str = (char *) malloc(num+1);
if(state==3){
str[0]="A";
} else if(state==10){
str[1]="B";
}
/* ... */
找到您的
if(state==3({
并尝试将上述内容放入此类中
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 代码使用向量成功运行,但使用数组显示错误
- C++逗号分隔的输入数组代码过早退出
- 如何修改代码以打印出数组 v2
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 如何在 c# 代码中通过引用调用时从 c++ dll 更新数组值?
- 使用基于数组和范围的 For 循环替换一些基本代码行
- G++ 发出警告,要求删除一个代码的数组,但不删除另一个代码的数组
- 为什么我的代码在尝试复制字符数组时引发 C6386 错误?
- 用于查找数组中最大元素的出现次数的代码,给出分段错误
- 调试代码块中的数组
- "p"数组如何使用 std::normal_distribution 存储以下代码中的值C++?
- 在带有模板的函数中传递未知大小的 std::数组.如何更正此代码?
- 有没有办法在c ++中清除空数组,请检查下面的代码以获取说明
- 我在 2D 数组的动态内存分配中遇到了一些奇怪的代码C++? 请解释一下这是什么?
- 我的旋转数组代码给出了大数组大小的错误
- 将字符数组转换为键代码数组 c++