【发布时间】:2013-04-28 16:37:44
【问题描述】:
我知道有很多双指针问题,但我找不到与启动数组有关的问题。
在下面的代码中,我可以通过ptrs[0] = &array[0]; 在main 中设置指针,但是当enqueue() 调用*queue[i] = p; 时代码会停止。这是为什么?我不知道这是否重要,但 ptrs[] 没有初始化。
#define QUEUE_LEN 5
int *ptrs[5];
int array[5] = {1,2,3,4,5};
void enqueue(int *p, int **queue) {
int i = 0;
int *tmp;
// Find correct slot
while (*queue && *queue[i] >= *p) {
i++;
}
// Error no free slots
if (i == QUEUE_LEN) {
printf("No free slots.\r\n");
return;
}
// Insert process
if (!*queue) {
*queue[i] = p;
return;
}
else {
tmp = *queue[i];
*queue[i] = p;
}
// Increment the other processes
return;
}
int main(int argc, char** argv) {
int i;
for (i=0; i<5; i++) {
enqueue(&array[i], ptrs);
}
for (i=0; i<QUEUE_LEN; i++)
printf("%d\n", *(ptrs[i]));
return 0;
}
【问题讨论】:
-
您的代码完全无法编译。在
*queue[i] = p中,您尝试将指针分配给整数。 -
嗯...似乎是我的编译器,它只是一个简单的复制粘贴到这里。
-
那么你应该得到一个合适的 C 编译器或尝试提高你的警告级别。即使它编译了,它也不会像你认为的那样做。
标签: c arrays pointers double-pointer