简体   繁体   English

循环遍历 C 中的指针数组?

[英]Looping through a pointer array in C?

I have a pointer array, and I want to set all values to 0 .我有一个指针数组,我想将所有值设置为0

Here is my code:这是我的代码:

#define EMPTY 0

void initBoard(int *ptr, int n){

n = n * n;
ptr = (int*)malloc(n*sizeof(int));
int i = 0;
for(i;i<n;i++){
    *ptr = EMPTY;
    *ptr++;
}
i = 0;
for(i;i<n;i++){
    printf("%d\n",*ptr);
    *ptr++;
}

}

when running this code, I expect to get (N == 0):运行此代码时,我希望得到 (N == 0):

0
0
0
0

instead I get this:相反,我得到了这个:

394066110
7321
6890384
6881472

Why am I getting this?为什么我得到这个?

EDIT:编辑:

void printBoard(int *ptr){

printf("%d - %d - %d\n", ptr, ptr+1, ptr+2);
printf("%d - %d - %d\n", ptr+3, ptr+4, ptr+5);
printf("%d - %d - %d\n", ptr+6, ptr+7, ptr+8);

}

will print out:将打印出:

0 - 4 - 8
12 - 16 - 20
24 - 28 - 32

here is how it looks when i call the funtion:这是我调用函数时的样子:

    int *ptr, win = 0;

initBoard(*ptr);
printBoard(*ptr);

EDIT 2:编辑2:

#include <stdio.h>
#include <stdlib.h>

//define
#define EMPTY 0
#define CROSS 1
#define CIRCLE 2
#define NO_WIN 0
#define PLAYER_WIN 1
#define COMPUTER_WIN 2
#define DRAW 3

//funktioner
void printBoard(int *ptr);
int checkWin(int *ptr);
void computerMove(int *ptr);
void initBoard(int *ptr);

int main()
{
    int *ptr, win = 0;

    initBoard(*ptr);
    printBoard(*ptr);

    return 0;
}

void printBoard(int *ptr){

    printf("%d - %d - %d\n", ptr[0], ptr[1], ptr[2]);


}

int checkWin(int *ptr){

    return 0;

}

void computerMove(int *ptr){

}

void initBoard(int *ptr){

    ptr = malloc(9*sizeof(int));
    int i = 0;
    //int *orig = ptr; //spara pointern
    for(i;i<9;i++){
        *ptr = EMPTY;
        ptr++;
    }
}

You misunderstand the way the dereference operator * and post-increment operator ++ interact.您误解了取消引用运算符*和后增量运算符++交互的方式。

When you write this *ptr++ you say "first, get me the writeable value at the pointer; then increment the pointer".当您编写此*ptr++您会说“首先,获取指针处的可写值;然后增加指针”。 This expression does not make sense by itself, because the result of dereferencing is discarded.这个表达式本身没有意义,因为解引用的结果被丢弃了。 In other words, it is equivalent to ptr++ .换句话说,它等价于ptr++

Now consider your second loop.现在考虑你的第二个循环。 Your pointer points to one past the end of the allocated block.您的指针指向已分配块末尾之后的位置。 At this point, dereferencing it is illegal.在这一点上,取消引用它是非法的。

You can fix this by storing the original pointer before coming into the first loop, and using the saved value to reset ptr before the second loop:您可以通过在进入第一个循环之前存储原始指针并使用保存的值在第二个循环之前重置ptr来解决此问题:

int i = 0;
int *orig = ptr; // Save the original pointer
for(i;i<n;i++){
    *ptr++ = EMPTY; // Combine ++ and * on assignment
}
i = 0;
ptr = orig; // Restore the original pointer
for(i;i<n;i++){
    printf("%d\n", *ptr++); // Combine ++ and * on read
}

If you would like initiBoard to initialize ptr that has not previously been set, change the signature as follows:如果你想让initiBoard初始化之前没有设置过的ptr ,修改签名如下:

void initBoard(int **ptrPtr, int n);

Call initBoard(&ptr, n) , and use ptrPtr with an extra level of dereference, ie调用initBoard(&ptr, n) ,并使用具有额外取消引用级别的ptrPtr ,即

*ptrPtr = malloc(n*sizeof(int));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM