gpt4 book ai didi

c - 语法错误 : missing ';' before 'type' IN C

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:14 24 4
gpt4 key购买 nike

我试图完成我的代码。在为数组分配内存之后。我得到的错误很少,因此我无法编译代码。我想得到你的帮助来完成这个程序,谢谢!

1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(27): error C2143: syntax error : missing ';' before 'type'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(28): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(28): warning C4047: '=' : 'int' differs in levels of indirection from 'char **'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(29): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(36): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(36): error C2109: subscript requires array or pointer type
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(38): error C2143: syntax error : missing ';' before 'type'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2109: subscript requires array or pointer type
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2198: 'gets' : too few arguments for call
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): warning C4047: 'function' : 'char **' differs in levels of indirection from 'int'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): warning C4024: 'PrintStr2' : different types for formal and actual parameter 1
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我的代码

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LINES 4
#define length 100

void SortText1(char **text1);
void SortText2(char **text2);
void PrintStr1(char **text1);
void PrintStr2(char **text2);
void main (){
int i;
char **text1;
text1=(char **)malloc(LINES*sizeof(char *));
if(!text1){ // Terms - if there is not enough memory,print error msg and exit the program.
printf("alloc failed\n");
return ;
}

for (i=0 ; i<LINES; i++){
text1[i] =(char*) malloc(length*sizeof(char));
}

char **text2;
text2=(char **)malloc(LINES*sizeof(char *));
if(!text2){ // Terms - if there is not enough memory,print error msg and exit the program.
printf("alloc failed\n");
return ;
}

for (i=0 ; i<LINES; i++){
text2[i] =(char*) malloc(length*sizeof(char));
}

char text3[LINES+LINES][length];
printf("First string,Please enter %d names (only capital letters,without spaces):", LINES);
for ( i=0 ; i<LINES ; i++){
gets(text1[i]);
}

printf("Second string,Please enter %d names (only capital letters,without spaces):", LINES);
for ( i=0 ; i<LINES ; i++){
gets(text2[i]);
}

PrintStr1(text1);
PrintStr2(text2);
SortText1(text1);
system("pause");
return ;
}
void SortText1(char **text1){
int i,j,k;
for (i=LINES-1 ; i>0 ; i--){
for ( j=0 ; j<i ; j++ ){
if(strcmp(text1[i],text1[i+1]) > 0 ){
char *temp=text1[i+1];
text1[i+1]=text1[i];
text1[i]=temp;
}
}
}

printf("The first string sorted:\n");
for ( k=0 ; i<LINES ; k++){
printf("%s ",text1[k]);
}
printf("\n");
}

void PrintStr1(char **text1){
int i;
printf("The first string of NAMES:\n");
for ( i=0 ; i<LINES ; i++){
printf("%s ",text1[i]);
}
printf("\n");
}
void PrintStr2(char **text2){
int i;
printf("The second string of NAMES:\n");
for ( i=0 ; i<LINES ; i++){
printf("%s ",text2[i]);
}
printf("\n");
}

最佳答案

您收到这些错误是因为您声明的局部变量不在作用域的开头。

     for (i=0 ; i<LINES; i++)
{
text1[i] =(char*) malloc(length*sizeof(char));
}
char **text2; // <<<<<<<<<<<<<<<<<<<
text2=(char **)malloc(LINES*sizeof(char *));

在范围中间声明变量的能力最早是在 C99 标准中引入的,MS 编译器不支持这种能力。

将所有局部变量声明移动到 { 之后和 block 中使用它们的任何其他语句之前。

关于c - 语法错误 : missing ';' before 'type' IN C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18912912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com