C2100 Error: Illegal Indirection
C2100 Error: Illegal Indirection
我不明白这个错误(C2100:非法间接寻址)。我标记了三个实例——都在底部附近。我在网上看过,我知道这与我的指示有关,但在 8 小时后,我完全迷路了。这里可能还有其他一些错误,但我什至无法分辨,因为我无法编译它。请帮忙,我希望得到一个我能理解的解释,这样我就可以弄清楚我在未来做错了什么。
// INCLUDE FILES
#include <stdio.h>
#include <string.h>
// PROGRAM CONSTANTS
#define MAX_MSG_LEN 81 // Maximum Message Length (Including /0 Character)
// FUNCTION PROTOTYPES
void printLength(int, int); // Function to Validate & Print Length of String
void printString(int, char); // Function to Print the String in Reverse
void writeToFile(int, char);
// GLOBAL VARIABLES
char input[MAX_MSG_LEN]; // Input String
int maxLength = MAX_MSG_LEN - 1; // Actual String Length (Not Including /0 Character)
char *ptr = input; // Character Pointer to String
int length = 0; // Length of Current String
int lcv = 0; // Loop Control Variable
void main()
{
FILE *ifp;
ifp = fopen("reverseString.txt", "w");
printf("\n\nEnter a String Between 1 and %d Characters: ", maxLength); // Prompts User to Enter a String Less Than 80
gets(input); // Receives the Inputted String from the User
length = strlen(input); // Counts the Length of the Inputted String & Assigns the Number to the "length" Variable
printLength(length, maxLength);
printString(length, *ptr);
writeToFile(length, *ptr);
}
void printLength(int length, int maxLength)
{
if(length > maxLength)
{
printf("\n\nThe Maximum Length of %d Characters was Exceeded!", maxLength);
printf("\nProgram Terminated...\n\n");
exit(0);
}
printf("\n\nThe Length of the Input String was: %d\n", length); // Prints the Length of the Inputted String
}
void printString(int length, char ptr)
{
for(; lcv < length; lcv++)
{
ptr++;
}
length = lcv;
printf("\nThe String in Reverse: ");
for(ptr--; length > 0; length--)
{
printf("%c", *ptr); // HERE IS ONE INSTANCE OF C2100
*ptr--; // HERE IS ONE INSTANCE OF C2100
}
printf("\n\n");
return;
}
void writeToFile(int length, char ptr)
{
FILE *ifp;
ifp = fopen("reverseString.txt", "w");
fprintf(ifp, "%c", *ptr); // HERE IS ONE INSTANCE OF C2100
fclose(ifp);
}
在你的代码中,你的语法是错误的。您需要更改声明和定义
void printString(int, char);
至
void printString(int, char*);
并这样称呼它
printString(length, ptr);
writeToFile()
函数也是如此。
否则,使用当前代码,在 printString()
和 writeToFile()
函数中,定义为 char ptr
,ptr
不是您可以解引用的指针类型.
也就是说,
切勿使用 gets()
,它会出现缓冲区溢出问题,请改用 fgets()
。
在使用 returned 指针之前,始终检查 fopen()
中的 return 值,以确保调用成功。
为了符合标准,void main()
至少应该是int main(void)
。
我尝试在 DevC++ 上编译您的代码 IDE,我发现您的代码存在 3 个问题。我会列出如下。
1) 尝试按照 Saurav Ghosh 的建议更改函数 printString()
和 writeToFile()
的声明。
2) 包含 stdlib.h
header 以支持 exit()
函数,因为 exit()
的定义在 stdlib.h
中可用
3)将void main(){/* your code*/}
改为int main(){/* your code */}
我做了上面的修改,它在我的机器上成功编译了。
我不明白这个错误(C2100:非法间接寻址)。我标记了三个实例——都在底部附近。我在网上看过,我知道这与我的指示有关,但在 8 小时后,我完全迷路了。这里可能还有其他一些错误,但我什至无法分辨,因为我无法编译它。请帮忙,我希望得到一个我能理解的解释,这样我就可以弄清楚我在未来做错了什么。
// INCLUDE FILES
#include <stdio.h>
#include <string.h>
// PROGRAM CONSTANTS
#define MAX_MSG_LEN 81 // Maximum Message Length (Including /0 Character)
// FUNCTION PROTOTYPES
void printLength(int, int); // Function to Validate & Print Length of String
void printString(int, char); // Function to Print the String in Reverse
void writeToFile(int, char);
// GLOBAL VARIABLES
char input[MAX_MSG_LEN]; // Input String
int maxLength = MAX_MSG_LEN - 1; // Actual String Length (Not Including /0 Character)
char *ptr = input; // Character Pointer to String
int length = 0; // Length of Current String
int lcv = 0; // Loop Control Variable
void main()
{
FILE *ifp;
ifp = fopen("reverseString.txt", "w");
printf("\n\nEnter a String Between 1 and %d Characters: ", maxLength); // Prompts User to Enter a String Less Than 80
gets(input); // Receives the Inputted String from the User
length = strlen(input); // Counts the Length of the Inputted String & Assigns the Number to the "length" Variable
printLength(length, maxLength);
printString(length, *ptr);
writeToFile(length, *ptr);
}
void printLength(int length, int maxLength)
{
if(length > maxLength)
{
printf("\n\nThe Maximum Length of %d Characters was Exceeded!", maxLength);
printf("\nProgram Terminated...\n\n");
exit(0);
}
printf("\n\nThe Length of the Input String was: %d\n", length); // Prints the Length of the Inputted String
}
void printString(int length, char ptr)
{
for(; lcv < length; lcv++)
{
ptr++;
}
length = lcv;
printf("\nThe String in Reverse: ");
for(ptr--; length > 0; length--)
{
printf("%c", *ptr); // HERE IS ONE INSTANCE OF C2100
*ptr--; // HERE IS ONE INSTANCE OF C2100
}
printf("\n\n");
return;
}
void writeToFile(int length, char ptr)
{
FILE *ifp;
ifp = fopen("reverseString.txt", "w");
fprintf(ifp, "%c", *ptr); // HERE IS ONE INSTANCE OF C2100
fclose(ifp);
}
在你的代码中,你的语法是错误的。您需要更改声明和定义
void printString(int, char);
至
void printString(int, char*);
并这样称呼它
printString(length, ptr);
writeToFile()
函数也是如此。
否则,使用当前代码,在 printString()
和 writeToFile()
函数中,定义为 char ptr
,ptr
不是您可以解引用的指针类型.
也就是说,
切勿使用
gets()
,它会出现缓冲区溢出问题,请改用fgets()
。在使用 returned 指针之前,始终检查
fopen()
中的 return 值,以确保调用成功。为了符合标准,
void main()
至少应该是int main(void)
。
我尝试在 DevC++ 上编译您的代码 IDE,我发现您的代码存在 3 个问题。我会列出如下。
1) 尝试按照 Saurav Ghosh 的建议更改函数 printString()
和 writeToFile()
的声明。
2) 包含 stdlib.h
header 以支持 exit()
函数,因为 exit()
的定义在 stdlib.h
3)将void main(){/* your code*/}
改为int main(){/* your code */}
我做了上面的修改,它在我的机器上成功编译了。