PDA

View Full Version : اشکال در تعریف اشاره گر به تابع ؟ و مقدار بازگشت ؟



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;
}
}
}

rahnema1
چهارشنبه 27 آذر 1392, 21:05 عصر
سلام
چند جا که مربوط به استفاده از اشاره گر ها بود اصلاح شد
نمی دونم از تابع add چه می خواهید ولی فکر کنم شرط های if که بررسی می کند head==null هنوز اضافه باشد


#include <stdio.h>
typedef struct node{
char *data;
struct node *link;
}node;

void add(node *,char *);
node* getNodePtr(char *);
void show( node* temp);
int main(int argc, char *argv[]) {
struct node f1;
node *first=NULL;// getNodePtr("\0");
printf("%d",sizeof(node));

add(first,"ehsan");
add(first,"omid");
add(first,"bita");

show(first);
printf("slam0");
getchar();
return 0;
}
void show( node* temp){
while(temp!=NULL){
printf("date:%s\n",temp->data);
temp=temp->link;
}
}
node* getNodePtr(char *s){
node* temp=(node*)malloc(sizeof(node));
temp->data=s;
temp->link=NULL;
return temp;
}
void add(node *head,char *s){
node* temp,*last=NULL;
node* newNode=(node*)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;
}
}
}

zehs_sha
پنج شنبه 28 آذر 1392, 07:25 صبح
با سلام ، تشکر تابع add نود ها را به لیست اضافه می کند که باز با توجه به تغییرات شما هنوز این امر میسد نشد ؟؟؟:گریه::گریه::گریه: یعنی وقتی تابع show قرار باشه لیست چاپ کنه نمی تونه بخاطر اینکه first هنوز null ؟؟؟

rahnema1
پنج شنبه 28 آذر 1392, 13:05 عصر
چند جای دیگه هم اصلاح شد


#include <stdio.h>
typedef struct node
{
char *data;
struct node *link;
}node;
void add(node ** head,char * s);
node* getNodePtr(char *);
void show( node* temp);
int main(int argc, char *argv[])
{
struct node f1;
node *first=NULL;

add(&first,"ehsan");
add(&first,"omid");
add(&first,"bita");

show(first);
getchar();
return 0;
}
void show( node* temp){
while(temp!=NULL){
printf("date:%s\n",temp->data);
temp=temp->link;
}
}
node* getNodePtr(char *s){

node* temp=(node*)malloc(sizeof(node));
temp->data=s;
temp->link=NULL;
return temp;
}
void add(node **head,char *s){
node* temp,*last=NULL;
node* newNode=(node*)malloc(sizeof(node));
if (s==NULL){
*head=getNodePtr("\0");
temp=*head;
}
if (*head==NULL){
*head=getNodePtr(s);
}
else{
temp=*head;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=getNodePtr(s);

}
}

omidshaman
پنج شنبه 28 آذر 1392, 17:48 عصر
آگر این قراره که link list باشه که خیلی مشکل داره !!!
هم memory leak داره هم از نظر پیاده سازی مشکل داره کلا !