-1

I'm trying to do some pointer arithmetic and for some reason, the pointer is not retaining the change in address after it exits the loop.

My output is the pointer address of the array (the WHOLE array) printed repeatedly not changing.

What it should be is printing the address of one value, and then after the dice roll will move the pointer by that many places forward in the array and print that next time through.

EDIT: I am not writing a linked list. An exact example of what I am trying to do can be found here: TutorialsPoint.com | C - Pointer arithmetic HOWEVER, while I can run that example code and have it work perfectly, my code fails every time.

EDIT 2: Complete source code was requested

ProjectName.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);

}

Header File:

#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 

Mechanics.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;
}
}
Citrus Rain
  • 119
  • 1
  • 4
  • 16
  • Why are you doing different things for `p1` and `p2`? – Daniel H Oct 12 '17 at 19:34
  • The arithmetic is working fine. You are not assigning the result anywhere. – Mad Physicist Oct 12 '17 at 19:35
  • everything in `c` is pass by value. When you pass `p1` and `p2` to `move`, local copies of those pointers are made in the function, and lost once the function exits. If you want to actually change the pointer values in `move`, you'll need to pass the addresses of `p1` and `p2` to `move` , and change its definition to `void move(char** p1, char** p2 ...)` – yano Oct 12 '17 at 19:36
  • 1
    Possible duplicate of [C: change pointer passed by value](https://stackoverflow.com/questions/5531765/c-change-pointer-passed-by-value) –  Oct 12 '17 at 19:37
  • Note: `p2 = &board;` --> semantically wrong to assign the address of an array to a `char *` (type mis-match) – chux - Reinstate Monica Oct 12 '17 at 19:49
  • The different things are just from trial and error. – Citrus Rain Oct 12 '17 at 19:51
  • Just tried the double pointer, did `void move(char **p1, char **p2, int turn, int boardSize)` and in debugging, the values are still staying exactly the same on every call to the function. – Citrus Rain Oct 12 '17 at 19:56
  • 1
    Please show the complete source code for your program. (I know this isn't it because the compiler would not recognize the identifier `NULL` without a `#include` directive for a header that defines it.) Also, `void main()` should be `int main(void)`, though that's almost certainly not the cause of your problem. – Keith Thompson Oct 12 '17 at 20:12

2 Answers2

0

Firstly, your p1 and p2 variables are not going to change across successive calls to the move function because you are passing the pointers by value. See What's the difference between passing by reference vs. passing by value?

Secondly, there are several other errors in the code you will need to address. This line is one of them. It doesn't do what your comment says.

int r = 1 + (rand() % 5);      // returns a pseudo-random integer from 1 to 6
MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
  • The very next line is to check to be sure that r is within that range, it does exactly what I commented. – Citrus Rain Oct 12 '17 at 20:15
  • `rand() % 5` is in the range 0 to 4. Adding 1 makes the range 1 to 5. How is that 1 to 6? – stark Oct 12 '17 at 20:39
  • Ah, I see. I had a moment of confusion, and I didn't exactly realize I was being told it was a smaller range than expected. – Citrus Rain Oct 12 '17 at 20:59
0

This whole thing was from me misreading what I was supposed to do. I was supposed to return a char pointer, instead of the function being void.

I'm embarrassed now and I wouldn't mind this being deleted.

Citrus Rain
  • 119
  • 1
  • 4
  • 16