0
基本的に以下のコードでは、いくつかの名前と年齢を含むリストを作成しようとしています。エラーや警告は表示されませんが、何も印刷されません。私が間違っていることは何ですか?関数の内部p
に割り当てリストの要素をC言語で挿入して印刷する
#include <stdio.h>
#include <stdlib.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array
*/
#define HOW_MANY 7
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
typedef struct person
{
char *name;
int age;
struct person *next;
}Person;
static void insert(Person *p, char *name, int age)
{
Person *headp = NULL;
p = (Person*)malloc(sizeof(Person));
if (p == NULL)
abort();
p->name = name;
p->age = age;
p->next = headp;
headp = p;
}
int main(int argc, char **argv)
{
Person *people=NULL;
for (int i = 0; i < 7; i++)
{
insert (people, names[i], ages[i]);
}
while (people != NULL)
{
printf ("name: %s, age: %i\n", people->name, people->age);
people = people->next;
}
return 0;
}
私はそれをどのように正確に行いますか?機能の挿入にはどのようなタイプが必要ですか? –
'p'の値を返す場合、' p'と同じ型になります。 –