我有一些语法问题,我不知道如何修复。我正在尝试为矩阵构建一些函数,有人可以帮我解决这些问题吗?
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");
}
}
答案 0 :(得分:6)
#define
是preprocessor 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
更改为其他变量名称。
那就是说,
gets()
严重受缓冲区溢出问题的影响。请改用fgets()
。malloc()
中C
和家人的返回值。main()
的推荐签名为int main(void)
。