大家好,我在想是否可以将指针值分配给变量?基本上我有指向整数值的指针,我想将它分配给另一个变量。IE:
coursep->node = 1 //coursep is pointer to structure and node is structure variable
int var;
var = coursep->node; //I want variable to be 1
我可以这样做吗?如何?当我尝试这样分配它时,它似乎不起作用。
编辑:基本上我试图从指针值到数组或结构。
lines = lineCount(courses); //counts how many lines does file have
struct courses course[lines]; // creates array of structures with amount of lines
for (i = 0; i < lines; i++) {
struct courses *coursep;
coursep = course;
fscanf(co, " %c %d ", &coursep->courseName, &coursep->numberOfNodes);
course[i].courseName = coursep->courseName;
course[i].numberOfNodes = coursep->numberOfNodes;
for (j = 0; j < coursep->numberOfNodes; j++) {
fscanf(co, " %d", &(coursep->nodes[j]));
var = *coursep->nodes[j];
printf("%d\t", var);
}
fscanf(co, "\n");
}
结构:
struct courses{
char courseName;
int numberOfNodes;
int nodes[];
};
我似乎无法将它直接放入数组中,因为它会打印出随机垃圾,如果我使用指针,它似乎显示正确的值(所有内容都是从文件中读取的),但是当我尝试将其全部放入数组时,它似乎会随机抛出再次垃圾=/有什么想法吗?