【问题标题】:Pointer Arithmetic not working指针算术不起作用
【发布时间】:2018-03-24 20:33:42
【问题描述】:

我正在尝试做一些指针运算,但由于某种原因,指针在退出循环后没有保留地址的变化。

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

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

编辑:我不是在写一个链表。可以在此处找到我正在尝试做的一个确切示例: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;
}
}

【问题讨论】:

  • 你为什么要为p1p2做不同的事情?
  • 算术工作正常。您没有在任何地方分配结果。
  • c 中的所有内容都是按值传递的。当您将p1p2 传递给move 时,这些指针的本地副本会在函数中生成,并且一旦函数退出就会丢失。如果要实际更改move 中的指针值,则需要将p1p2 的地址传递给move,并将其定义更改为void move(char** p1, char** p2 ...)
  • 请显示您的程序的完整源代码。 (我知道这不是因为如果没有 #include 指令来定义它的标头,编译器将无法识别标识符 NULL。)此外,void main() 应该是 int main(void),尽管这几乎可以肯定不是你的问题的原因。

标签: c pointers


【解决方案1】:

这整件事是因为我误读了我应该做的事情。 我应该返回一个 char 指针,而不是函数是 void。

我现在很尴尬,我不介意被删除。

【讨论】:

    【解决方案2】:

    首先,您的 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
    

    【讨论】:

    • 下一行是检查以确保 r 在该范围内,它完全符合我的评论。
    • rand() % 5 在 0 到 4 的范围内。加 1 使范围为 1 到 5。1 到 6 是怎么算的?
    • 啊,我明白了。我有片刻的困惑,我并没有完全意识到我被告知它的范围比预期的要小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 2015-06-17
    • 1970-01-01
    • 2022-01-23
    • 2010-09-28
    • 2012-07-27
    相关资源
    最近更新 更多