所以基本上在下面的代码中我试图创建一个包含一些名称和年龄的列表。我没有收到任何错误或警告,但它没有打印任何内容。我做错了什么?
#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;
}
答案 0 :(得分:1)
在函数内部分配@Resource(name="jdbc/YourDataSource")
Datasource ds;
不会从main更改p
,这就是为什么在打印时应该people
仍然是people
的原因。< / p>
您可以从NULL
返回p
的新值,并将该值指定给insert
。