我使用指針來存放名稱和研究實驗室屬性。但是,當我打印現有的頂點,當我打印頂點時,我無法正確看到所謂的屬性。 例如,雖然名字的真正價值在於「蘭斯洛特」,我認爲這是錯誤的,例如「asdasdasdasd」用於char的指針問題*
struct vertex {
int value;
char*name;
char* researchLab;
struct vertex *next;
struct edge *list;
};
void GRAPHinsertV(Graph G, int value,char*name,char*researchLab) {
//create new Vertex.
Vertex newV = malloc(sizeof newV);
// set value of new variable to which belongs the person.
newV->value = value;
newV->name=name;
newV->researchLab=researchLab;
newV->next = G->head;
newV->list = NULL;
G->head = newV;
G->V++;
}
/***
The method creates new person.
**/
void createNewPerson(Graph G) {
int id;
char name[30];
char researchLab[30];
// get requeired variables.
printf("Enter id of the person to be added.\n");
scanf("%d",&id);
printf("Enter name of the person to be added.\n");
scanf("%s",name);
printf("Enter researc lab of the person to be added\n");
scanf("%s",researchLab);
// insert the people to the social network.
GRAPHinsertV(G,id,name,researchLab);
}
void ListAllPeople(Graph G)
{
Vertex tmp;
Edge list;
for(tmp = G->head;tmp!=NULL;tmp=tmp->next)
{
fprintf(stdout,"V:%d\t%s\t%s\n",tmp->value,tmp->name,tmp->researchLab);
}
system("pause");
}
'strdup'可能不是ISO C,但它是POSIX,所以它的唯一的「非標「在」選擇「等的意義上說。人。是「非標準的」。 – 2010-03-17 20:26:29
@Tyler,是的,我在'strdup'上做我的作業,並且意識到它不像我想的那樣是GNU擴展,而是POSIX標準的一部分。不過,我認爲這條評論最好留在評論中。 – strager 2010-03-17 20:29:45