如何在使用for循环和数组时更改为c代码 < >我使用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]; } }

如何在使用for循环和数组时更改为c代码 < >我使用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]; } },c++,c,arrays,string,for-loop,C++,C,Arrays,String,For Loop,我尝试使用数组,但无法获得列表输出 #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

我尝试使用数组,但无法获得列表输出

 #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])
     }
}

由于字符串长度将在编译后决定,因此将由操作系统在运行时设置

 /* ...   */


    char str[]; 
    str = (char *) malloc(num+1);

  if(state==3){
    str[0]="A";
  } else if(state==10){
    str[1]="B";
  } 

/* ...    */
找到你的

ifstate==3{


并尝试在上面的

中进行介绍。让我们从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
    }
因此,您有两个警告,其中一个是实际错误,但更糟糕的是,您还有5个错误。所有这些都告诉您要修复什么

你的主要问题是字符串!你尝试将C++类型字符串转换成C类型字符串的方式是错误的。 C中没有字符串类型

所以

这实际上是C语言中的一个错误

C字符串是字符数组的一种特殊用法。特殊用法是字符数组中的一个元素必须具有值“\0”即NUL。然后,该元素发出字符串结束的信号

请考虑这个:

char strA[2] = "A"; // strA[0] is 'A' and strA[1] is '\0'
通过这种方式,您已使C字符串等于a。注意:字符串长度为1,但数组仍需要两个字符。这是因为“\0”需要额外的字符来表示字符串的结尾

因此,如果希望一个包含两个字符串的数组保存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]);

使用C++但不能将它更改为C-NaAH -但是为什么?它不是完美的IS?你是否得到了一个特定的错误?开始把你的编译器的警告级别变成一个高级别,例如GCC墙…然后修复所有警告。BTW:注意C++版本有bug。
strcpy(str[0], "A");  // instead of str[0] = "A"
puts(str[0]);  // or puts(str[1]);