我正在尝试完成一个C编程主题的高校作业。Solaris编译器使我的C代码行为怪异
我已经在我的MacBook上完成了我的任务,并且我的代码完全按照它的设计工作。然而,我们的讲师已经告诉我们,我们所有的代码都将使用大学的Solaris 10 Server进行编译,并且我们被告知如果您的代码不能编译和运行,将会扣除标记。
我们也接到指示,我们的应用程序将通过以下语法标记编译:
gcc -ansi -Wall -pedantic assign1.c -lm -o assign1
使用和执行:
./assign1
我的代码目前编译没有错误或警告并正确执行。但是,我的某个函数在此Solaris机器上无法正常工作。该函数应该是我们自己实现的基本字符串排序 - 用户输入一个长达40个字符的字符串,它应该被转换为小写字母并按照字母顺序排序,并删除所有非字母字符。
我的Mac上,它输出:
Enter a string (1-40 characters): asdfghjkl
Output: adfghjkls
在学院的Solaris系统,它输出:
Enter a string (1-40 characters): asdfghjkl
Output: aa#?dfghjkls
我的函数,它(大多数)输出的是:
void sortLine(int *optionStats, char* source)
{
char tempsort[MAX_SORT_LENGTH + 1];
char comp_c;
int i,j,k;
char c = source[i++];
i = j = k = 0;
optionStats[2]++;
while (c != '\n' && c != '\0' && c != EOF) {
/* convert uppercase characters to lowercase */
if ((int)c >= (int)'A' && (int)c <= (int)'Z')
c = c + ((int)'a' - (int)'A');
/* if the character is alphabeic then sort it else skip it */
if ((int)c <= (int)'z' && (int)c >= (int)'a') {
for (j = 0; j <= MAX_SORT_LENGTH + 1; j++) {
comp_c = tempsort[j];
if (comp_c == '\n' || comp_c == '\0' || comp_c == EOF) {
tempsort[j] = c;
break;
}
if ((int)c <= (int)comp_c) {
for (k = MAX_SORT_LENGTH + 1; k > j; k--) {
tempsort[k] = tempsort[k - 1];
}
tempsort[j] = c;
break;
}
}
}
c = source[i++];
}
/* copy the sorted temporary array into the source array */
for (i = 0; i <= MAX_SORT_LENGTH + 1; i++) {
source[i] = tempsort[i];
}
}
但是有一些注意事项:
函数定义本身( 签名)由 讲师提供,因此名称,返回类型, 参数等不能被改变。 ,我们必须使用它(但我们 可以做任何我们想要的内部)。
的代码必须是ANSI/C90兼容 (GRR!)
谁能帮我找出什么被吐出了这个功能造成这些奇怪的额外的字符 - 它做我的头?
我的猜测是一个编译器初始化了一些东西(用0),而其他的不用。例如变量'i' – Anycorn 2011-04-05 04:35:19
此代码可能会导致[鼻恶魔](http://www.catb.org/jargon/html/N/nasal-demons.html) – 2011-04-05 04:39:08
感谢您的出色建议,但我解决了这个问题 - 但错误仍然发生。 :( – Ash 2011-04-05 04:44:26