我正在嘗試完成一個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