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


 

code:

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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM