所以我才刚刚开始编写代码。在我写了一小部分之后,我去测试了所写的内容。结果我什至不知道该怎么解释。
#include <stdio.h>
#include <stdlib.h>
struct window {
unsigned left, top;
unsigned width, height;
unsigned zorder;
};
/*struct kurs {
unsigned left, top; //ok, this isn't important
};*/
int main() {
//the program starts before int, my console window opens up
struct window *wnd;
//struct kurs pokazivac; this isn't important also
int number_wnd;
//then it just skips to int number_wnd line, and when I try to go to next step, it stays there, console windows opens up, asking some kind of input
//if I press enter it skips to this empty line, and if I press next step it asks me some kind of input again
printf("Number of windows: "); //when I press eneter it comes to beginning of this line,and when I press next step it moves to scanf
scanf("%d", &number_wnd); //when I try to go next step from beginning of this line it opens up exe.common.inl
wnd = (struct window*)malloc(number_wnd * sizeof(struct window));
if (wnd == NULL) {
printf("Memory problem!");
return 0;
}
printf("\nInsert window positions: ");
for (int i = 0;i < number_wnd;++i) { //this isn't important also, since it doesn't even get to here
printf("%d. WINDOW\n", i + 1);
printf("\tleft: ");
scanf("%u", wnd[i].left);
printf("\ttop: ");
scanf("%u", wnd[i].top);
printf("\twidth: ");
scanf("%u", wnd[i].width);
printf("\theight: ");
scanf("%u", wnd[i].height);
printf("\tzorder: ");
scanf("%u", wnd[i].zorder);
putchar('\n');
}
if (wnd != NULL) {
free(wnd);
}
return 0;
}
这是我弄乱代码的结果,预结果(在结构kurs上没有/ ** /)甚至更奇怪。代码从该结构的末尾开始,后来它会报告错误,即使关闭,我也需要向左}
关闭。
我正在Visual Studio中编写此代码,并在2个开放项目中进行了尝试。 我的视觉工作室坏了吗?还是我的代码坏了?
所以,我编译了它;现在我得到了错误:
LNK1104无法打开文件'C:\ Users \ Korisnik \ Desktop \ VSITE \ PMA \ Labosi \ lab10 \ Debug \ lab10.exe'
我应该如何对待?将粘贴代码复制到新项目还是?这是我第一次见面,而且我是编程的新手,所以我真的没有很多知识。
我将&
放在了scanf
中,甚至都不知道我怎么忽略了它。但是并没有改变问题。代码仍然在main之前开始。
答案 0 :(得分:1)
这里
scanf("%u", wnd[i].left); /* you are missing & */
您要访问wnd[i]
,但是您仅为wnd
分配了内存,而没有为wnd[i]
分配内存。代替使用结构指针,使用结构指针数组。对于例如
struct window *wnd[number_wnd];
以便您可以为每个wnd
分配内存并将数据放入其中。
示例代码:
struct window {
unsigned left, top;
unsigned width, height;
unsigned zorder;
};
int main(void) {
int number_wnd;
printf("Number of windows: \n");
scanf("%d", &number_wnd);
struct window *wnd[number_wnd]; /* array of structure pointer */
printf("\nInsert window positions: ");
for (int i = 0;i < number_wnd;++i) {
printf("%d. WINDOW\n", i + 1);
wnd[i] = (struct window*)malloc(sizeof(struct window)); /* for each wnd allocate memory equal to size of window */
if (wnd[i] == NULL) {
printf("Memory problem!");
return 0;
}
printf("\tleft: ");
scanf("%u", &wnd[i]->left); /* you need to give & as left is an integere */
printf("\ttop: ");
scanf("%u", &wnd[i]->top);
printf("\twidth: ");
scanf("%u", &wnd[i]->width);
printf("\theight: ");
scanf("%u", &wnd[i]->height);
printf("\tzorder: ");
scanf("%u", &wnd[i]->zorder);
putchar('\n');
}
for (int i = 0;i < number_wnd;++i) {
free(wnd[i]); /* free each wnd */
}
return 0;
}
答案 1 :(得分:0)
1-我在编译器上使用了相同的代码,并且工作得很好,我只需要更改以下内容
`
scanf("%u", &wnd[i].left);
printf("\ttop: ");
scanf("%u", &wnd[i].top);
printf("\twidth: ");
scanf("%u", &wnd[i].width);
printf("\theight: ");
scanf("%u", &wnd[i].height);
printf("\tzorder: ");
scanf("%u", &wnd[i].zorder);`