| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:[初學者問題] C pointer to struct
只看楼主 加入收藏
fjctp
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-2-24
收藏
 问题点数:0 回复次数:3 
[初學者問題] C pointer to struct
此程式是製作一個linked list (First in last out)
但是在 add_to_list 這個 Function 內有點問題
每次 add_to_list 這個 Function 修改 list->value & list->next 的時候
只要一離開 add_to_list, 修改就會無效了, 變回預設值
所以我想請教一下有關 pointer's scope & 解決的辦法
謝謝



程序代码:
/* testing a list */
#include <stdio.h>
#include <stdlib.h>

struct data
{
        int value;
        struct data* next;
};

int menu();
void call_related_function (int choice, struct data* list);
void add_to_list (struct data* list);
void remove_from_list (struct data* list);
void print_list (struct data* list);

/*
name:main
para: none
return: none
work:         1) print a menu        (F)
                3) call related function        (F)
*/

int main()
{
        int choice=0;
        struct data* list = malloc(sizeof(struct data));
        if (list==NULL)
        {
                printf("ERROR!!! Not enough memory.\n");
                abort;
        }
       

        /* initializing list */
        list->value=-1;
        list->next=NULL;

        /* Work */
        printf(        "This is a FILO list demonsration.\n");
        do
        {
                choice=menu();
                call_related_function (choice, list);
        } while (choice!=4);
       

        return 0;
}


/*
name: menu (main step 1)
para: none
return: int choice
work:        1)         print a) add a value to list
                                  b) remove a value from the list
                                  c) print the list
                2)        get user's choice
                3) return user's choice
*/
int menu()
{
        printf(        "1) Add a value to list.\n"
                        "2) Remove a value from list.\n"
                        "3) print the list.\n"
                        "4) Exit.\n"
                        "Please enter your choice: "
                        );
        int choice = 0;
        scanf("%d", &choice);
       

        return choice;
}

void call_related_function (int choice, struct data* list)
{
        switch (choice)
        {
                case 1:
                        add_to_list(list);
                        break;
                       

                case 2:
                        remove_from_list(list);
                        break;
                       

                case 3:
                        print_list(list);
                        break;
                       

                case 4:
                        break;
                       

                default:
                        printf("ERROR!!!! Wrong Input");
                        break;
        }
}
                       

/*
name: add_to_list
para: struct data* list
return: none
work:        1) ask for a value from user
                2) Create a tmp pointer to list
                3) allocate space for tmp pointer
                4) set tmp pointer
                5) set list pointer to tmp pointer
*/

void add_to_list (struct data* list)
{
        printf("Value: ");
        int value;
        scanf("%d", &value);
       

        /* For the first value */
        if(list->value == -1)
                list->value = value;
        /* For the other value */
        else
        {
                struct data* tmp = malloc(sizeof(struct data));
                if(tmp==NULL)
                {
                        printf("ERROR!!! Not enough memory.\n");
                        abort;
                }
              

                /* Add to list */
                tmp->value = value;
                tmp->next = list;
                list = tmp;
        }
}

/*
name: remove_from_list
para: struct data* list
return: none
work:        1) create a tmp pointer
                2) set it pointing to the list
                3) set list to tmp->next
                4) free tmp
*/

void remove_from_list (struct data* list)
{
        if(list->next!=NULL)
        {
                struct data* tmp = list;
                list = tmp->next;
                free(tmp);
        }
}

/*
name: print_list
para: struct data* list
return: none
work:        1) create a tmp pointer pointer to list
                2) print -> if i >0                (L)
                2) print tmp->vale
*/

void print_list (struct data* list)
{
        struct data* tmp = list;
        int i=0;
        printf("The list: ");
        do
        {
                if(i>0)
                        printf(" -> ");
                printf("%d", tmp->value);
                i++;
        }while(tmp->next!=NULL);
        printf("\n");
}

搜索更多相关主题的帖子: linked pointer 
2011-02-24 05:45
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
/* testing a list */
#include <stdio.h>
#include <stdlib.h>

struct data
{
        int value;
        struct data* next;
};

int menu();
struct data *call_related_function (int choice, struct data* list);
struct data *add_to_list (struct data* list);
struct data *remove_from_list (struct data* list);
void print_list (struct data* list);

/*
name:main
para: none
return: none
work:         1) print a menu        (F)
              3) call related function        (F)
*/

int main()
{
        int choice=0;
        struct data* list =(struct data*)malloc(sizeof(struct data));
        if (list==NULL)
        {
                printf("ERROR!!! Not enough memory.\n");
                exit(0);
        }
      

        /* initializing list */
        list->value=-1;
        list->next=NULL;

        /* Work */
        printf("This is a FILO list demonsration.\n");
        do
        {
                choice=menu();
                list=call_related_function (choice, list);
        } while (choice!=4);
      
        system("pause");
        return 0;
}


/*
name: menu (main step 1)
para: none
return: int choice
work:        1)         print a) add a value to list
                                 b) remove a value from the list
                                  c) print the list
                2)        get user's choice
                3) return user's choice
*/
int menu()
{
        printf(        "1) Add a value to list.\n"
                        "2) Remove a value from list.\n"
                        "3) print the list.\n"
                        "4) Exit.\n"
                        "Please enter your choice: "
                        );
        int choice = 0;
        scanf("%d", &choice);
      

        return choice;
}

struct data *call_related_function (int choice, struct data* list)
{
        switch (choice)
        {
                case 1:
                        list=add_to_list(list);
                        break;
                       

                case 2:
                        list=remove_from_list(list);
                        break;
                       

                case 3:
                        print_list(list);
                        break;
                       

                case 4:
                        break;
                       

                default:
                        printf("ERROR!!!! Wrong Input");
                        break;
        }
        return list;
}
                       

/*
name: add_to_list
para: struct data* list
return: none
work:        1) ask for a value from user
                2) Create a tmp pointer to list
                3) allocate space for tmp pointer
                4) set tmp pointer
                5) set list pointer to tmp pointer
*/

struct data *add_to_list (struct data* list)
{
        printf("Value: ");
        int value;
        scanf("%d", &value);
      

        /* For the first value */
        if(list->value == -1)
                list->value = value;
        /* For the other value */
        else
        {
                struct data* tmp =(struct data*)malloc(sizeof(struct data));
                if(tmp==NULL)
                {
                        printf("ERROR!!! Not enough memory.\n");
                        exit(0);
                }
              

                /* Add to list */
                tmp->value = value;
                tmp->next = list;
                list = tmp;
        }
        return list;
}

/*
name: remove_from_list
para: struct data* list
return: none
work:        1) create a tmp pointer
                2) set it pointing to the list
                3) set list to tmp->next
                4) free tmp
*/

struct data *remove_from_list (struct data* list)
{
        if(list->next!=NULL)
        {
                struct data* tmp = list;
                list = tmp->next;
                free(tmp);
        }
        return list;
}

/*
name: print_list
para: struct data* list
return: none
work:        1) create a tmp pointer pointer to list
                2) print -> if i >0                (L)
                2) print tmp->vale
*/

void print_list (struct data* list)
{
        struct data* tmp = list;
        int i=0;
        printf("The list: ");
        do
        {
                if(i>0)
                printf(" -> ");
                printf("%d", tmp->value);
                i++;
        }while(tmp->next!=NULL);
        printf("\n");
}

   唯实惟新 至诚致志
2011-02-24 07:20
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
你的问题在于不更新list指针,每一次执行增加或者删除指针list指针始终停在原来的位置,也就是最后一个位置。
所以在各个函数要及时返回新的第一个指针的位置(你的方法是向前插入的)
。我修改后的程序还有一个问题。你的方法是向前插入的。
就是只能打印出一个指针的内容,因为有其他的事,这个工作就交给你了,不好意思。

   唯实惟新 至诚致志
2011-02-24 07:25
fjctp
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-2-24
收藏
得分:0 
原來我一直誤會了
我一直都忘了我的目標是改 pointer
謝謝 :)
2011-02-25 11:17
快速回复:[初學者問題] C pointer to struct
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043969 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved