1
mainと関数expandからなるプログラムを作成しました。問題は、コードをコンパイルしてXcode(最新バージョン)で実行したときに意図した結果を返すことですが、コンパイルしてgccコンパイラで実行すると、実行直後にコードがスタックされます(警告もエラーもありません)。これは、私が端末のコードをコンパイルするために使用するコマンドです:gccとllvmコンパイラではCプログラムの動作が異なります
gcc expand.c -o expand -Wall -pedantic -ansi
以下は私のコードです。私は私の問題が何であるか分かりません:
#include <stdio.h>
#define MAX_LEN 100
#define ATOI_GAP 48
void expand(char s1[], char s2[]);
int main()
{
int i;
char s2[MAX_LEN]; /* declare the target array */
char s1[4]; /* declare the source array */
s1[0] = 'a';
s1[1] = '-';
s1[2] = 'z';
s1[3] = '\0';
for(i = 0; i < MAX_LEN; ++i) { /* print s2 array */
printf("%d ", s2[i]);
}
expand(s1, s2);
for(i = 0; s2[i] != '\0'; ++i) { /* print s2 array */
printf("%c ", s2[i]);
}
return 0;
}
/* the function gets string s1 of format "letterX-letterY"
and fills the "-" with the consequent letters from letterX to
letterY. For example, if s1 = "a-d", then s2 will be "abcd"*/
void expand(char s1[], char s2[]) {
int start = s2[0] = s1[0]; /* the first letter of the array s2 is the same as that of the array s1 */
int stop = s1[2]; /* determine at which letter we need to stop */
int j;
printf("inside expand");
for(j = 1; j < stop - '0' - ATOI_GAP; ++j) {
s2[j] = ++start; /* fill in the gap */
}
s2[j] = '\0';
printf("finished expand");
}
'/ *ターゲット配列を初期化する* /' ... umm..where? –
@SouravGhosh私の悪い、それはメインで初期化されていない、ちょうど宣言した。それでも問題は解決しません。 – Yos
@Yos: 'gcc version 4.8.5 20150623(Red Hat 4.8.5-4)(GCC)'と 'Apple LLVM version 8.0.0(clang-800.0.42.1)'をコンパイルして実行します。 –