如何在使用for循环和数组时更改为c代码

how to change to c code while using for loop and array

本文关键字:代码 数组 循环 for      更新时间:2023-10-16

我使用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({

并尝试将上述内容放入此类中

相关文章: