关于C和C++的 语句转换

来源:百度知道 编辑:UC知道 时间:2024/05/07 12:35:27
各位师兄师姐大家好,我是一个C++的初学者,我想问下在 C环境下的语句p = (struct node *)malloc(sizeof(struct node));在C++里应该用什么语句实现?

应该是 node *p=new node;
这样就可以了
node 就像int一样,是一种数据类型

可以直接用c的语法!
非要用c++语法,则p=new node();

#include<stdio.h>
#include<stdlib.h>
struct node
{
int i;
};

int main(void)
{
struct node* p_c = (struct node *)malloc(sizeof(struct node));
struct node* p = new node();

printf("%d,%d\n", p_c->i, p->i);
return 0;
}

struct node* p = new (struct node);
有点记不清了,你试试看?