これは私が and を使って書いた最初のプログラムmalloc()
ですfree()
。私には正しいように見え、私の本を参照すると、本の例と非常によく似ているように見えます。ただし、プログラムを実行すると、(lldb)
プロンプトが表示されます。
たとえば、要素数に 8、初期化値に 2 を入力します。私の xcode コンパイラは「(lldb)」と返します。
誰かが私を正しい方向に導くことができますか?
#include <stdio.h>
#include <stdlib.h>
int * make_array(int elem, int val);
void show_array(const int ar[], int n);
int main(void)
{
int *pa;
int size;
int value;
printf("Enter the number of elements: ");
scanf("%d", &size);
while (size > 0) {
printf("Enter the initialization value: ");
scanf("%d", &value);
pa = make_array(size, value);
if (pa)
{
show_array(pa, size);
free (pa);
}
printf("Enter the number of elements (<1 to quit): ");
scanf("%d", &size);
}
printf("Done.\n");
return 0;
}
int * make_array(int elem, int val)
{
int index;
int * ptd;
ptd = (int *) malloc(elem * sizeof (int));
for (index = 0; index < elem; index++)
ptd[index] = val;
return ptd;
}
void show_array(const int ar[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d",ar[i]);
}