How to set fill out an array using memsetHow do I determine the size of my array in C?How do you set, clear, and toggle a single bit?How to initialize all members of an array to the same value?With arrays, why is it the case that a[5] == 5[a]?How do function pointers in C work?How to pass an array of struct using pointer in c/c++?Pointers to string in functionsUsing printf on an array of chars returns garbage and stringUnable to access Struct contents properlyWhy does compiler see a mismatch between char * and printf's conversion specifier “s” when the char * is typedef'd and accessed through a struct?
Can Brexit be undone in an emergency?
How was ownership of property managed during the Black Death, when so many original owners had died?
As a discovery writer, how to complete unfinished novel (which is highly diverted from original plot ) after a time-gap
How could artificial intelligence harm us?
How should I avoid someone patenting technology in my paper/poster?
Algorithm for competing cells of 0s and 1s
Why are two-stroke engines nearly unheard of in aviation?
Why are Fuji lenses more expensive than others?
Is there any actual security benefit to restricting foreign IPs?
Does Mage Hand give away the caster's position?
Minimize taxes now that I earn more
Do the villains know Batman has no superpowers?
Do household ovens ventilate heat to the outdoors?
What did the controller say during my approach to land (audio clip)?
Lead Amalgam as a Material for a Sword
I reverse the source code, you negate the output!
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
Removing rows containing NA in every column
Can さくじつ and きのう be used the same way?
Quick Kurodoko Puzzle: Threes and Triples
Floating Point XOR
I was cheated into a job and want to leave ASAP, what do I tell my interviewers?
Applications of mathematics in clinical setting
Why would a fighter use the afterburner and air brakes at the same time?
How to set fill out an array using memset
How do I determine the size of my array in C?How do you set, clear, and toggle a single bit?How to initialize all members of an array to the same value?With arrays, why is it the case that a[5] == 5[a]?How do function pointers in C work?How to pass an array of struct using pointer in c/c++?Pointers to string in functionsUsing printf on an array of chars returns garbage and stringUnable to access Struct contents properlyWhy does compiler see a mismatch between char * and printf's conversion specifier “s” when the char * is typedef'd and accessed through a struct?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a Player struct that contains a GameBoard, which is a 2d array of chars.
When I use memset it does not set the array
I have tried using the address of the board (&, *) and without those symbols
I can get it to work if I use a pointer to the Player variable in 'initializeBoard' but the assignment says not to use a pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
const int ROWS = 10;
const int COLS = 10;
const char *PLAYERONE = "Player 1";
const char *PLAYERTWO = "Player 2";
const char WATER = '~';
const int NUM_SHIPS = 5;
typedef struct gameBoard
char board[ROWS][COLS];
GameBoard;
typedef struct human
char name[20];
GameBoard gameBoard;
Player;
// function prototypes
void displayGameBoard(Player player);
void initializeBoard(Player player);
void initializePlayer(Player *player, const char *name);
// main function
int main()
Player playerOne;
Player playerTwo;
// call function welcomeScreen
initializePlayer(&playerOne, PLAYERONE);
// end program
return 0;
void displayGameBoard(Player player) 2
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
void initializePlayer(Player *player, const char *name)
char playerName[20];
GameBoard playerBoard;
strcpy(player->name, playerName);
player->gameBoard = playerBoard;
initializeBoard(*player);
displayGameBoard(*player);
I expect the result to be filled with '~' but it either corrupts the whole terminal or prints blanks
c
|
show 5 more comments
I have a Player struct that contains a GameBoard, which is a 2d array of chars.
When I use memset it does not set the array
I have tried using the address of the board (&, *) and without those symbols
I can get it to work if I use a pointer to the Player variable in 'initializeBoard' but the assignment says not to use a pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
const int ROWS = 10;
const int COLS = 10;
const char *PLAYERONE = "Player 1";
const char *PLAYERTWO = "Player 2";
const char WATER = '~';
const int NUM_SHIPS = 5;
typedef struct gameBoard
char board[ROWS][COLS];
GameBoard;
typedef struct human
char name[20];
GameBoard gameBoard;
Player;
// function prototypes
void displayGameBoard(Player player);
void initializeBoard(Player player);
void initializePlayer(Player *player, const char *name);
// main function
int main()
Player playerOne;
Player playerTwo;
// call function welcomeScreen
initializePlayer(&playerOne, PLAYERONE);
// end program
return 0;
void displayGameBoard(Player player) 2
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
void initializePlayer(Player *player, const char *name)
char playerName[20];
GameBoard playerBoard;
strcpy(player->name, playerName);
player->gameBoard = playerBoard;
initializeBoard(*player);
displayGameBoard(*player);
I expect the result to be filled with '~' but it either corrupts the whole terminal or prints blanks
c
1
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
1
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
Don't copy the struct by value. You got it right ininitializePlayer
but wrong ininitializeBoard
. Voting to close this as simple typo.
– Lundin
Mar 28 at 14:11
When you callstrcpy(player->name, playerName);
,playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you meanstrcpy(player->name, name);
?
– Jabberwocky
Mar 28 at 14:15
|
show 5 more comments
I have a Player struct that contains a GameBoard, which is a 2d array of chars.
When I use memset it does not set the array
I have tried using the address of the board (&, *) and without those symbols
I can get it to work if I use a pointer to the Player variable in 'initializeBoard' but the assignment says not to use a pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
const int ROWS = 10;
const int COLS = 10;
const char *PLAYERONE = "Player 1";
const char *PLAYERTWO = "Player 2";
const char WATER = '~';
const int NUM_SHIPS = 5;
typedef struct gameBoard
char board[ROWS][COLS];
GameBoard;
typedef struct human
char name[20];
GameBoard gameBoard;
Player;
// function prototypes
void displayGameBoard(Player player);
void initializeBoard(Player player);
void initializePlayer(Player *player, const char *name);
// main function
int main()
Player playerOne;
Player playerTwo;
// call function welcomeScreen
initializePlayer(&playerOne, PLAYERONE);
// end program
return 0;
void displayGameBoard(Player player) 2
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
void initializePlayer(Player *player, const char *name)
char playerName[20];
GameBoard playerBoard;
strcpy(player->name, playerName);
player->gameBoard = playerBoard;
initializeBoard(*player);
displayGameBoard(*player);
I expect the result to be filled with '~' but it either corrupts the whole terminal or prints blanks
c
I have a Player struct that contains a GameBoard, which is a 2d array of chars.
When I use memset it does not set the array
I have tried using the address of the board (&, *) and without those symbols
I can get it to work if I use a pointer to the Player variable in 'initializeBoard' but the assignment says not to use a pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
const int ROWS = 10;
const int COLS = 10;
const char *PLAYERONE = "Player 1";
const char *PLAYERTWO = "Player 2";
const char WATER = '~';
const int NUM_SHIPS = 5;
typedef struct gameBoard
char board[ROWS][COLS];
GameBoard;
typedef struct human
char name[20];
GameBoard gameBoard;
Player;
// function prototypes
void displayGameBoard(Player player);
void initializeBoard(Player player);
void initializePlayer(Player *player, const char *name);
// main function
int main()
Player playerOne;
Player playerTwo;
// call function welcomeScreen
initializePlayer(&playerOne, PLAYERONE);
// end program
return 0;
void displayGameBoard(Player player) 2
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
void initializePlayer(Player *player, const char *name)
char playerName[20];
GameBoard playerBoard;
strcpy(player->name, playerName);
player->gameBoard = playerBoard;
initializeBoard(*player);
displayGameBoard(*player);
I expect the result to be filled with '~' but it either corrupts the whole terminal or prints blanks
c
c
edited Mar 28 at 14:05
JPHamlett
asked Mar 28 at 13:58
JPHamlettJPHamlett
1117 bronze badges
1117 bronze badges
1
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
1
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
Don't copy the struct by value. You got it right ininitializePlayer
but wrong ininitializeBoard
. Voting to close this as simple typo.
– Lundin
Mar 28 at 14:11
When you callstrcpy(player->name, playerName);
,playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you meanstrcpy(player->name, name);
?
– Jabberwocky
Mar 28 at 14:15
|
show 5 more comments
1
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
1
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
Don't copy the struct by value. You got it right ininitializePlayer
but wrong ininitializeBoard
. Voting to close this as simple typo.
– Lundin
Mar 28 at 14:11
When you callstrcpy(player->name, playerName);
,playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you meanstrcpy(player->name, name);
?
– Jabberwocky
Mar 28 at 14:15
1
1
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
1
1
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
Don't copy the struct by value. You got it right in
initializePlayer
but wrong in initializeBoard
. Voting to close this as simple typo.– Lundin
Mar 28 at 14:11
Don't copy the struct by value. You got it right in
initializePlayer
but wrong in initializeBoard
. Voting to close this as simple typo.– Lundin
Mar 28 at 14:11
When you call
strcpy(player->name, playerName);
, playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you mean strcpy(player->name, name);
?– Jabberwocky
Mar 28 at 14:15
When you call
strcpy(player->name, playerName);
, playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you mean strcpy(player->name, name);
?– Jabberwocky
Mar 28 at 14:15
|
show 5 more comments
2 Answers
2
active
oldest
votes
Here:
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
You are passing the Player
object by copy rather than reference. The object you are setting, is not the object you intend to set, and is in any event temporary. Passing large structures on the stack is also inefficient (you may want to reconsider the signature of displayGameBoard()
too in that case). Also the member board
does not need to be dereferenced.
void initializeBoard(Player* player);
...
void initializeBoard(Player* player)
memset( player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
Then call it in initializePlayer()
thus:
initializeBoard(player);
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
add a comment
|
The problem is, that you pass your Player-structure by value here:
initializeBoard(*player);
so you essentially are working on a copy. Pass a pointer and work on the original structure like this:
initializeBoard(player);
and change the initializeBoard-function to that:
void initializeBoard(Player *player)
memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
then it should work as expected. You should change the displayBoard()
function, too, because there is no reason to pass by value and it is much more computation effort (copy the whole structure)
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399427%2fhow-to-set-fill-out-an-array-using-memset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here:
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
You are passing the Player
object by copy rather than reference. The object you are setting, is not the object you intend to set, and is in any event temporary. Passing large structures on the stack is also inefficient (you may want to reconsider the signature of displayGameBoard()
too in that case). Also the member board
does not need to be dereferenced.
void initializeBoard(Player* player);
...
void initializeBoard(Player* player)
memset( player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
Then call it in initializePlayer()
thus:
initializeBoard(player);
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
add a comment
|
Here:
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
You are passing the Player
object by copy rather than reference. The object you are setting, is not the object you intend to set, and is in any event temporary. Passing large structures on the stack is also inefficient (you may want to reconsider the signature of displayGameBoard()
too in that case). Also the member board
does not need to be dereferenced.
void initializeBoard(Player* player);
...
void initializeBoard(Player* player)
memset( player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
Then call it in initializePlayer()
thus:
initializeBoard(player);
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
add a comment
|
Here:
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
You are passing the Player
object by copy rather than reference. The object you are setting, is not the object you intend to set, and is in any event temporary. Passing large structures on the stack is also inefficient (you may want to reconsider the signature of displayGameBoard()
too in that case). Also the member board
does not need to be dereferenced.
void initializeBoard(Player* player);
...
void initializeBoard(Player* player)
memset( player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
Then call it in initializePlayer()
thus:
initializeBoard(player);
Here:
void initializeBoard(Player player)
memset(*player.gameBoard.board, WATER, sizeof(player.gameBoard.board));
You are passing the Player
object by copy rather than reference. The object you are setting, is not the object you intend to set, and is in any event temporary. Passing large structures on the stack is also inefficient (you may want to reconsider the signature of displayGameBoard()
too in that case). Also the member board
does not need to be dereferenced.
void initializeBoard(Player* player);
...
void initializeBoard(Player* player)
memset( player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
Then call it in initializePlayer()
thus:
initializeBoard(player);
edited Mar 28 at 16:59
answered Mar 28 at 14:12
CliffordClifford
64.1k9 gold badges70 silver badges130 bronze badges
64.1k9 gold badges70 silver badges130 bronze badges
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
add a comment
|
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
Already answered by @Ctx
– Trickzter
Mar 28 at 14:13
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter That doesn't matter, he obviously started writing it before I posted it. I also absolutely dislike being in a hurry just to be the first to post (+1).
– Ctx
Mar 28 at 14:14
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Trickzter : What is your point? Check out the post timing. The answer was not posted when I started writing this - I cannot help it if there is one right answer and two people were concurrently answering it! Besides, two people explaining the same thing may give different views or add clarity.
– Clifford
Mar 28 at 14:16
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Clifford I get that but for the sake of clearity and simplicity I think the redundancy is not necessary. You both clearified the same situation in the same way. Just wanted to keep this topic clear! I know that things like this happen easily and are common :)
– Trickzter
Mar 28 at 14:17
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
@Trickzter : I am not about to delete the post because it was duplicated by a race condition. Your SO rep does not indicate that you are qualified to be the arbiter of such things. The way to raise the profile of an answer you like is to vote for it. This duplication is harmless, and it seems now preferred by the OP in any case.
– Clifford
Mar 28 at 14:25
add a comment
|
The problem is, that you pass your Player-structure by value here:
initializeBoard(*player);
so you essentially are working on a copy. Pass a pointer and work on the original structure like this:
initializeBoard(player);
and change the initializeBoard-function to that:
void initializeBoard(Player *player)
memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
then it should work as expected. You should change the displayBoard()
function, too, because there is no reason to pass by value and it is much more computation effort (copy the whole structure)
add a comment
|
The problem is, that you pass your Player-structure by value here:
initializeBoard(*player);
so you essentially are working on a copy. Pass a pointer and work on the original structure like this:
initializeBoard(player);
and change the initializeBoard-function to that:
void initializeBoard(Player *player)
memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
then it should work as expected. You should change the displayBoard()
function, too, because there is no reason to pass by value and it is much more computation effort (copy the whole structure)
add a comment
|
The problem is, that you pass your Player-structure by value here:
initializeBoard(*player);
so you essentially are working on a copy. Pass a pointer and work on the original structure like this:
initializeBoard(player);
and change the initializeBoard-function to that:
void initializeBoard(Player *player)
memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
then it should work as expected. You should change the displayBoard()
function, too, because there is no reason to pass by value and it is much more computation effort (copy the whole structure)
The problem is, that you pass your Player-structure by value here:
initializeBoard(*player);
so you essentially are working on a copy. Pass a pointer and work on the original structure like this:
initializeBoard(player);
and change the initializeBoard-function to that:
void initializeBoard(Player *player)
memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
then it should work as expected. You should change the displayBoard()
function, too, because there is no reason to pass by value and it is much more computation effort (copy the whole structure)
answered Mar 28 at 14:07
CtxCtx
12.7k8 gold badges22 silver badges39 bronze badges
12.7k8 gold badges22 silver badges39 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399427%2fhow-to-set-fill-out-an-array-using-memset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
its better to provide small piece of code which is applicable only for your problem. Its time consuming to look through it
– Alex
Mar 28 at 13:59
1
If you know the problem is the memset call, is there a good reason to post quite that much code?
– Clifford
Mar 28 at 14:02
I cut down the amount of code
– JPHamlett
Mar 28 at 14:03
Don't copy the struct by value. You got it right in
initializePlayer
but wrong ininitializeBoard
. Voting to close this as simple typo.– Lundin
Mar 28 at 14:11
When you call
strcpy(player->name, playerName);
,playerName
hasn't been initialized -> undefined behaviour. Fix that first. Did you meanstrcpy(player->name, name);
?– Jabberwocky
Mar 28 at 14:15