我有这个代码尝试使用整数的指针,并有一个初始化的警告使得指针来自整数而没有编译时编译它.此外,似乎编译后.exe文件将无法正常运行.请帮忙!
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char* argv[]) {
int num1=10, *num2=1;
printf("num1=%d \t num2=%d \n", num1, *num2);
*num2 = num1;
printf("num1=%d \t num2=%d \n", num1, *num2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
typedef struct vertex_t* Vertex;
struct vertex_t {
int id;
char *str;
};
int main(int argc, char* argv[])
{
int size = 10;
Vertex* vertexList = (Vertex*)malloc(size * sizeof(Vertex));
vertexList[0]->id = 5;
vertexList[0]->str = "helloworld";
printf("id is %d", vertexList[0]->id);
printf("str is %s", vertexList[0]->str);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
嗨!我正在尝试使用malloc来获取Vertex数组.当我运行该程序时,它没有打印出任何内容,并说该程序已停止运行.但是如果我只给了vertexList [0] - > id而不是vertexList [0] - > str并且只打印了vertexList [0]的值,它会打印出"id is 5"......然后程序停止运行.所以我觉得我对malloc部分做错了什么?:/提前谢谢你的帮助!
例如:
int f1() {
return 3;
}
void f2(int *num) {
*num = 3;
}
int n1, n2;
n1 = f1();
f2(&n2);
Run Code Online (Sandbox Code Playgroud)
使用f1,我们可以返回一个值并执行"variable = f1()"但是可以使用void函数完成相同的操作,该函数在给定其地址的情况下更新该变量的值,而不必执行"variable = f1()".
那么,这是否意味着我们实际上只能将void函数用于一切?或者是否有一些void函数无法替换另一个int函数/(类型)函数?