zehs_sha
چهارشنبه 27 آذر 1392, 17:31 عصر
با سلام به همه چرا وقتی اشاره گر زیر را به تابع ارسال و در تابع ساخته می شود باز گردانده نمی شود و همان مقدار null است در صورتی که جنس خودش از اشاره گر تعریف شده قطعه کد زیر
typedef struct Node *node_ptr;
typedef struct Node{
char *data;
node_ptr *link;
}node;
void add(node_ptr *,char *);
node_ptr getNodePtr(char *);
void show(node_ptr);
int main(int argc, char *argv[]) {
node_ptr *first=NULL;// getNodePtr("\0");
printf("%d",sizeof(node));
add(first,"ehsan");
add(first,"omid");
add(first,"bita");
show(first);
getchar();
return 0;
}
void show(node_ptr temp){
while(temp!=NULL){
printf("date:%s\n",temp->data);
temp=temp->link;
}
}
node_ptr getNodePtr(char *s){
node_ptr temp=(node_ptr)malloc(sizeof(node));
temp->data=s;
temp->link=NULL;
return temp;
}
void add(node_ptr head,char *s){
node_ptr temp,last=NULL;
node_ptr newNode=(node_ptr)malloc(sizeof(node));
if (head==NULL){
head=getNodePtr("\0");
}
temp=head;
newNode=getNodePtr(s);
if (head==NULL){
head=newNode;
}else{
while(temp!=NULL){
last=temp;
temp=temp->link;
}
if (last!=NULL){
last=last->link=newNode;
}
}
}
typedef struct Node *node_ptr;
typedef struct Node{
char *data;
node_ptr *link;
}node;
void add(node_ptr *,char *);
node_ptr getNodePtr(char *);
void show(node_ptr);
int main(int argc, char *argv[]) {
node_ptr *first=NULL;// getNodePtr("\0");
printf("%d",sizeof(node));
add(first,"ehsan");
add(first,"omid");
add(first,"bita");
show(first);
getchar();
return 0;
}
void show(node_ptr temp){
while(temp!=NULL){
printf("date:%s\n",temp->data);
temp=temp->link;
}
}
node_ptr getNodePtr(char *s){
node_ptr temp=(node_ptr)malloc(sizeof(node));
temp->data=s;
temp->link=NULL;
return temp;
}
void add(node_ptr head,char *s){
node_ptr temp,last=NULL;
node_ptr newNode=(node_ptr)malloc(sizeof(node));
if (head==NULL){
head=getNodePtr("\0");
}
temp=head;
newNode=getNodePtr(s);
if (head==NULL){
head=newNode;
}else{
while(temp!=NULL){
last=temp;
temp=temp->link;
}
if (last!=NULL){
last=last->link=newNode;
}
}
}