gpt4 book ai didi

c - 语法错误: missing ')' before 'constant'

转载 作者:行者123 更新时间:2023-11-30 18:22:05 32 4
gpt4 key购买 nike

我有一些语法问题,我不知道如何解决。我正在尝试构建一些矩阵函数,有人可以帮助我解决这些问题吗?

1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(11): error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(11): error C2143: syntax error : missing '{' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(11): error C2059: syntax error : '<Unknown>'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(11): error C2059: syntax error : ')'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(12): error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(12): error C2143: syntax error : missing '{' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(12): error C2059: syntax error : '<Unknown>'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(12): error C2059: syntax error : ')'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(13): error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(13): error C2143: syntax error : missing '{' before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(13): error C2059: syntax error : '<Unknown>'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(13): error C2059: syntax error : ')'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(27): warning C4013: 'initMatrix' undefined; assuming extern returning int
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(29): warning C4013: 'completeMatrix' undefined; assuming extern returning int
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(30): warning C4013: 'displayMatrix' undefined; assuming extern returning int before 'constant'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(146): error C2059: syntax error : '<Unknown>'
1>c:\documents and settings\one\my documents\visual studio 2010\projects\hw3\hw3\q2.c(146): error C2059: syntax error : ')'

这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define N 10
#define MAX_CHAR 9
int checkString(char *str);
void replacingLetters(char *str);
int countWords(char *str);
int * initMatrix(int M, int N);
void completeMatrix(char *str,int *arr,int M, int N);
void displayMatrix(int *arr,int M, int N);
int main()
{
int *res;
int numOfWords;
char inputFromUser[N*MAX_CHAR];
printf("Please insert a string : ");
gets(inputFromUser);

if (1 == checkString(inputFromUser))
{
replacingLetters(inputFromUser);
numOfWords = countWords(inputFromUser);

*res = initMatrix(26,numOfWords);

completeMatrix(inputFromUser,res,26,numOfWords);
displayMatrix(res,26,numOfWords);
}

return 0;
}

int checkString(char *str)
{
int stringLen = 0;
int i = 0;

stringLen = strlen(str);

for (i = 0 ; i < stringLen ; ++i)
{
if ((str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= 'A' && str[i] <= 'Z') ||
(str[i] == ' '))
{
continue;
}

return 0;
}

return 1;
}

void replacingLetters(char *str)
{
int stringLen = 0;
int i = 0;

stringLen = strlen(str);

for (i = 0 ; i < stringLen ; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] - 'A' + 'a';
}
}
}

int countWords(char *str)
{
int stringLen = 0;
int i = 0;
int numberOfWords = 0;
int isNewWord = 1;

stringLen = strlen(str);

for (i = 0 ; i < stringLen ; ++i)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
if (isNewWord == 1)
{
numberOfWords++;
isNewWord = 0;
}
}

if (str[i] == ' ')
{
isNewWord = 1;
}
}

return numberOfWords;
}

int * initMatrix(int M, int N)
{
int * retVal = 0;
int i,j;
retVal = (int *)malloc(sizeof(int) *N *M);

for ( i = 0 ; i < N ; ++i)
{
for ( j = 0 ; j < M ; ++j)
{
retVal[i*M + j] = 0;
}
}
return retVal;
}

void completeMatrix(char *str,int *arr,int M, int N)
{
int numOfWords = 0;
int wordIdx = 0;
int idx = 0;
int idxInWord = 1;

if (1 == checkString(str))
{
replacingLetters(str);
numOfWords = countWords(str);

for (wordIdx = 0; wordIdx < numOfWords ; ++wordIdx)
{
idxInWord = 1;
while((str[idx] != 0) && (str[idx] != ' '))
{
arr[wordIdx * M + str[idx] - 'a'] = arr[wordIdx * M + str[idx] - 'a']*10 + idxInWord;
idx++;
idxInWord++;
}

idx++;
}
}
}

void displayMatrix(int *arr,int M, int N)
{
int i,j;

for ( i = 0 ; i < _N ; ++i)
{
for ( j = M -1 ; j >= 0 ; --j)
{
printf("%d ",_arr[i*M + j]);
}
printf("\n\n");
}
}

最佳答案

#definepreprocessor MACRO它在预处理阶段充当文本替换。在你的情况下,使用像这样的宏

#define N 10

然后使用类似的函数

void displayMatrix(int *arr,int M, int N);

事实证明

void displayMatrix(int *arr,int M, int 10);  //note the int 10 part

这基本上是将所有出现的N替换为10,之后它就变得非法了。您需要为函数参数使用 int 变量类型,而不是 int 文字。

将函数参数中出现的所有 int N 更改为其他变量名称。

也就是说,

  1. gets() 受到缓冲区溢出问题的严重影响。使用fgets()相反。
  2. Do not cast Cmalloc() 及其族的返回值。
  3. main() 的推荐签名为 int main(void)

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

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