code:

Cannot initialize a variable of type 'Stu *' with an rvalue of type 'void *'

Stu* pStu = malloc(sizeof(Stu));

改为
Stu* pStu = (Stu*)malloc(sizeof(Stu));

 

code

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

typedef struct {
    int a;
    int b;
}Stu;

Stu* getStu(int x, int y)
{
    Stu* pStu = (Stu*)malloc(sizeof(Stu));
    pStu->a = x;
    pStu->b = y;
    return pStu;
}

int main()
{
    int x = 2, y = 3;
    Stu *pStu = getStu(x, y);
    printf("%d %d\n", pStu->a, pStu->b);
    free(pStu);
    return 0;
}

输出

2 3
Program ended with exit code: 0

 


https://blog.csdn.net/weixin_34221332/article/details/86981433

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
  • 2021-07-05
  • 2021-12-04
  • 2022-12-23
  • 2021-12-10
  • 2021-08-05
猜你喜欢
  • 2022-12-23
  • 2022-01-06
  • 2021-07-08
  • 2021-09-18
  • 2021-12-30
  • 2021-10-26
  • 2022-12-23
相关资源
相似解决方案