c - 指针算术不起作用

标签 c pointers

我正在尝试进行一些指针算术,由于某种原因,指针在退出循环后没有保留地址的更改。

我的输出是重复打印的数组(整个数组)的指针地址,没有改变。

它应该是打印一个值的地址,然后在掷骰子之后将指针在数组中向前移动很多位置并在下次打印。

编辑:我不是在写链接列表。我想要做的一个确切的例子可以在这里找到:TutorialsPoint.com | C - Pointer arithmetic然而,虽然我可以运行该示例代码并使其完美工作,但我的代码每次都会失败。

编辑2:请求完整的源代码

项目名称.cpp

#include "TableOfContents.h"
void main()
{
FILE *fp;       //file reader
char board[100] = "  mHk nH l B He Flq p H  hByHlho H B  jr HFB ir j H  F ku gd  H pjB mH x  BF i H  m oB HlHFBhoH BB ";
char *p1, *p2; // player 1 and player 2
int turn = 1;
srand(time(NULL));   // should only be called once

              //    int z[10]; // example of how to make an array
/*
fp = fopen("C:\\Users\\ritol\\Documents\\cities1.txt", "r");
inputLine = "";
inputP = &inputLine; // set inputP to the address for inputLine
*/
p1 = &board[0];
p2 = &board[0];

fp = fopen("C:\\Users\\ritol\\Documents\\outputGameBoard.txt", "w");

while (turn <= 45) //
{
    //output the current turn

    //call the function move for p1
    move(*p1, *p2, turn, board);

    turn++;//TODO: only put this in it's correct place

    //call the function move for p2
    move(*p1, *p2, turn, board);
    //increment turn
    turn++;

    //call output function to output current board to disk file
    output(board, p1, p2, fp);
}

//determine and output the winner

//Wait
scanf("%d", &turn);

}

头文件:

#pragma once
#ifndef TOC
#define TOC
#define _CRT_SECURE_NO_DEPRECATE 
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
/*
Outputs the current game board to file
*/
void output(char *board, char *p1, char *p2, FILE *myfile);

/*
Receives 
*p1 - player 1 pointer
*p2 - player 2 pointer
turn - the argument to determine which player to move
*board - the size of the board
Outputs the player number, randomly generates a move from 1-6 for the player,
determines if the player has landed on an event, and moves accordingly,
checks and handles player collison,
outputs the amount the player moved and where they are now
*/
void move(char **p1, char **p2, int turn, int boardSize);

#endif 

力学.cpp

#include "TableOfContents.h"

void output(char *board, char *p1, char *p2, FILE *myfile)
{
//the loop will exist once  you have reached \0
fprintf(myfile, "%s\n", board);
//TODO: show the player locations 
}


/*
Moves a player and outputs the result of each players move
*/
void move(char **p1, char **p2, int turn, int boardSize)
{
//roll the dice

int r = (int) (1 + (rand() % 5));      // returns a pseudo-random integer     from 1 to 6

//REMOVE: tracer code
if (r > 6 || r == 0) printf("The dice have sinned.\n");

//Apply the dice roll based on whose turn it is
if (turn % 2 == 1)//Odd turn; Player 1's turn
{
    printf("%p ", p1 + r);
    *p1++;
}
else if (turn % 2 == 0) //Even turn; Player 2's turn
{
    printf("%p\n", p2);
    p2 += r;
}
}

最佳答案

首先,您的 p1 和 p2 变量不会在连续调用 move 函数时发生变化,因为您是按值传递指针。请参阅What's the difference between passing by reference vs. passing by value?

其次,您需要解决代码中的其他几个错误。这条线就是其中之一。它不符合您的评论所述。

int r = 1 + (rand() % 5);      // returns a pseudo-random integer from 1 to 6

关于c - 指针算术不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717493/

相关文章:

c++ - 在 C++ 中将函数指针中的 const 参数转换为非 const

c++ - 这是通过指针从 uint8_t vector 加载对象的安全方法吗?

c - getchar() 在 printf() 之前执行

C void指针和指针比较

c - 如果在C中退出(exitcode)会发生内存泄漏?

C 程序 - 在指针变量中存储值

C++:指针和圆括号 - 这是什么意思?

c - SPOJ COINS DP 和递归方法

c - 我收到段错误 : 11

html - 输入按钮的光标将无法设置为指针