如何创建和调用函数指针

发布于 2024-12-13 21:22:39 字数 1009 浏览 6 评论 0原文

嗨,我是一名初学者,我正在尝试找出一些指向函数示例的指针。我什至无法编译我的代码,它显示以下消息。我无法确定为什么会出现编译错误。

/tmp/cc0qghbo.o: In function `main':
pointer_to_function_inside_structure.c:(.text+0x88): undefined reference to `func_ptr'
collect2: ld returned 1 exit status

这是我的代码,请告诉我我做错了什么

#include<stdio.h>
#include<stdlib.h>

struct student_data
{
        char *name;
        int roll_num;
        int marks;
        void (* func_ptr)(struct student_data *ptr);
};

void print_data(struct student_data *ptr);

void print_data(struct student_data *ptr)
{
        printf("\nNAME OF THE STUDENT      %s", ptr -> name);
        printf("\nROLL NUMBER OF STUDENT   %d", ptr -> roll_num);
        printf("\nMARKS OF STUDENT         %d\n", ptr -> marks);
}

int main()
{
        struct student_data *ptr;

        ptr -> name = "ajish";
        ptr -> roll_num = 2;
        ptr -> marks = 50;

        ptr -> func_ptr = &print_data;
        func_ptr(ptr);
}

Hi I'm a beginner and I'm trying to work out some pointer to function examples. I can't even compile my code, it shows the following message. I cannot determine why I am getting the compilation errors.

/tmp/cc0qghbo.o: In function `main':
pointer_to_function_inside_structure.c:(.text+0x88): undefined reference to `func_ptr'
collect2: ld returned 1 exit status

here is my code, please tell me what I'm doing wrong

#include<stdio.h>
#include<stdlib.h>

struct student_data
{
        char *name;
        int roll_num;
        int marks;
        void (* func_ptr)(struct student_data *ptr);
};

void print_data(struct student_data *ptr);

void print_data(struct student_data *ptr)
{
        printf("\nNAME OF THE STUDENT      %s", ptr -> name);
        printf("\nROLL NUMBER OF STUDENT   %d", ptr -> roll_num);
        printf("\nMARKS OF STUDENT         %d\n", ptr -> marks);
}

int main()
{
        struct student_data *ptr;

        ptr -> name = "ajish";
        ptr -> roll_num = 2;
        ptr -> marks = 50;

        ptr -> func_ptr = &print_data;
        func_ptr(ptr);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2024-12-20 21:22:39
  1. 您没有使用指针分配内存。
  2. 由于 func_ptrstudent_data 的成员,因此您必须使用 struct Student_data *ptr 调用函数,该函数应该是指向结构实例的指针。

例子:

int main()
{
    // allocate memory
    struct student_data *ptr = malloc(sizeof(student_data));

    ptr -> name = "ajish";
    ptr -> roll_num = 2;
    ptr -> marks = 50;

    ptr -> func_ptr = &print_data;

    // change this to:
    ptr->func_ptr(ptr);

   // free memory
   free(ptr);
}
  1. You're not allocating memory using your pointer.
  2. Since func_ptr is a member of student_data you have to call your function using struct student_data *ptr which should be pointer to the instance of your struct.

Example:

int main()
{
    // allocate memory
    struct student_data *ptr = malloc(sizeof(student_data));

    ptr -> name = "ajish";
    ptr -> roll_num = 2;
    ptr -> marks = 50;

    ptr -> func_ptr = &print_data;

    // change this to:
    ptr->func_ptr(ptr);

   // free memory
   free(ptr);
}
我要还你自由 2024-12-20 21:22:39

您需要将 main() 的右大括号之前的最后一行更改为

func_ptr(ptr);

更改

ptr -> func_ptr(ptr);

后,程序为我编译并运行。我用gcc 4.5.0编译。

您还应该

  1. 在堆上为ptr分配空间

    struct Student_data *ptr = malloc(sizeof(struct Student_data));
    

    不要忘记在程序结束时释放它:

    免费(ptr);
    

    或者

  2. 声明ptr堆栈。

    struct Student_data ptr;
    

    这将要求您将 ptr 上的所有 -> 运算符更改为 . 运算符。

You need to change the last line before main()'s closing brace from

func_ptr(ptr);

to

ptr -> func_ptr(ptr);

After that change, the program compiled and ran for me. I compiled with gcc 4.5.0.

You should also either

  1. Allocate space for ptr on the heap

    struct student_data *ptr = malloc(sizeof(struct student_data));
    

    Don't forget to free it at the end of your program:

    free(ptr);
    

    Or

  2. Declare ptr on the stack.

    struct student_data ptr;
    

    This will require you to change all your -> operators on ptr to . operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文