attendu identifiant ou ‘(’ avant ‘/’ token
J'ai écrit le code suivant:
#include <stdio.h>
//To use fgets and printf I need to include stdio header
#include <string.h>
//To use strlen I need to include string header
#define MAXLINE 100 /* maximum input line length */
int suffix(char str[], char c);
/*
The function suffix prints all the substrings that
(1) starts with the letter stored in the variable 'c'
(2) included in the string str[]
Function should return the number of substrings.
*/
int main()
{
char user_input[MAXLINE]; /* current input line */
char ch = 0;
int counter = 0;
printf(" Please enter a string (up to 100 characters) and then press enter ");
/*
This will store a maximum of MAXLINE characters into the string from the standard input
and it will ensure it is null-terminated.
The fact that we've limited the input to the size of the array declared earlier
ensures that there's no possibility of buffer overruns.
*/
fgets(user_input, MAXLINE, stdin);
//Then let's clear the pesky newline that's been entered into the string using strlen:
user_input[strlen(user_input)-1] = '#include <stdio.h>
//To use fgets and printf I need to include stdio header
#include <string.h>
//To use strlen I need to include string header
#define MAXLINE 100 /* maximum input line length */
int suffix(char str[], char c);
/*
The function suffix prints all the substrings that
(1) starts with the letter stored in the variable 'c'
(2) included in the string str[]
Function should return the number of substrings.
*/
int main()
{
char user_input[MAXLINE]; /* current input line */
char ch = 0;
int counter = 0;
printf(" Please enter a string (up to 100 characters) and then press enter ");
/*
This will store a maximum of MAXLINE characters into the string from the standard input
and it will ensure it is null-terminated.
The fact that we've limited the input to the size of the array declared earlier
ensures that there's no possibility of buffer overruns.
*/
fgets(user_input, MAXLINE, stdin);
//Then let's clear the pesky newline that's been entered into the string using strlen:
user_input[strlen(user_input)-1] = '\0';
printf(" Please enter a character and then press enter ");
ch = getchar();
while ( getchar() != '\n' );
counter = suffix(user_input,ch);
return 0;
}
int suffix(char str[], char c);
{
int i=0,j=0;
for (i=0 ; (i < MAXLINE-1) ; i++)
{
if (str[i] == c)
for (j=i ; (j < MAXLINE-1) ; j++)
printf("%c", str[j]);
count++;
printf("\n");
}
return count;
}
';
printf(" Please enter a character and then press enter ");
ch = getchar();
while ( getchar() != '\n' );
counter = suffix(user_input,ch);
return 0;
}
int suffix(char str[], char c);
{
int i=0,j=0;
for (i=0 ; (i < MAXLINE-1) ; i++)
{
if (str[i] == c)
for (j=i ; (j < MAXLINE-1) ; j++)
printf("%c", str[j]);
count++;
printf("\n");
}
return count;
}
J'ai aussi écrit le faire fichier:
mystr: my_str.o
gcc -g -ansi -Wall my_str.o -o mystr
my_str.o: my_str.c
gcc -c -ansi -Wall my_str.c -o my_str.o
Cependant, je reçois la suite des erreurs de compilation:
VirtualBox:~/Desktop/Exercises/Assignments/my_str$ make
gcc -c -ansi -Wall my_str.c -o my_str.o
my_str.c:2:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c:5:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c: In function ‘main’:
my_str.c:38:5: error: expected expression before ‘/’ token
my_str.c:38:15: warning: character constant too long for its type [enabled by default]
my_str.c:45:5: warning: implicit declaration of function ‘suffix’ [-Wimplicit-function-declaration]
my_str.c:25:9: warning: variable ‘counter’ set but not used [-Wunused-but-set-variable]
my_str.c: At top level:
my_str.c:52:5: error: conflicting types for ‘suffix’
my_str.c:52:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
my_str.c:45:15: note: previous implicit declaration of ‘suffix’ was here
my_str.c:53:1: error: expected identifier or ‘(’ before ‘{’ token
make: *** [my_str.o] Error 1
VirtualBox:~/Desktop/Exercises/Assignments/my_str$
S'il vous plaît aider avec "attendu identifiant ou ‘(’ avant ‘/’ token" erreur.
Pourquoi conflicting types de suffixe? pourquoi la déclaration implicite (chaque fonction en C doivent être déclarées au début du programme, j'ai donc ce n'est pas clair pour moi ce qui est faux)?
Grâce
Vous devez vous connecter pour publier un commentaire.
Vous compilez avec
-ansi
qui, selonman gcc
désactive les commentaires de style C++ (//
):En plus de ce clcto a dit, vous devez supprimer le point-virgule à partir du début de la définition de fonction:
Et vous avez besoin de déclarer la variable
count
avant de l'utiliser:Ensuite votre code de la compilation (si elle ne peut pas faire ce que vous attendez).
J'ai appliqué toutes vos suggestions + faites un peu plus d'supplémentaire de bugs, et maintenant il fonctionne comme un charme.