I have some syntax problems that I don't know how to fix. I'm trying to build some functions to matrix, can someone help me resolve those problems?
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 : ')'
Here is the code:
#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");
}
}
#define
is a preprocessor MACRO which acts as a textual replacement in the preprocesssing stage. In your case, with a MACRO likeand then with a function like
it turns out to be
which is basically replacing all occurrences of
N
with10
and after which it becomes illegal. You need to have anint
variable type for function parameter, not anint
literal.Change all occurrences of
int N
in function parameter to some other variable name.That said,
gets()
suffers seriously from buffer overflow issue. Usefgets()
instead.malloc()
and family inC
.main()
isint main(void)
.