Why does printf behave this way when using struct?When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Why are mutable structs “evil”?When to use struct?Why does printf not flush after the call unless a newline is in the format string?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why Choose Struct Over Class?Why does the error struct FILE* but argument is of type char* appear?in a struct, scanf returns var address not valueSegmentation Fault when calling member of a struct
Remove ads in Viber for PC
Can a country avoid prosecution for crimes against humanity by denying it happened?
Calculus Books, preferably Soviet.
Solve this icositetragram
How to check status of Wi-Fi adapter through command line?
What is the maximal acceptable delay between pilot's input and flight control surface actuation?
Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code
How to add some symbol (or just add newline) if the numbers in the text are not continuous
Why would a Intel 8080 chip be destroyed if +12 V is connected before -5 V?
Initializing a std::array with a constant value
properties that real numbers hold but complex numbers does not
Can an intercepting fighter jet force a small propeller aircraft down without completely destroying it?
Punishment in pacifist society
Why is k-means used for non normally distributed data?
In Toy Story, are toys the only inanimate objects that become alive? And if so, why?
How can I design a magically-induced coma?
Ideal characterization of almost convergence
Meaning of "offen balkon machen"?
Function of the separated, individual solar cells on Telstar 1 and 2? Why were they "special"?
Are manifolds admitting a circle foliation covered by manifolds with a (non-trivial) circle action?
Where is the correct position to set right or left of muscle names for anatomical names?
How to run a command 1 out of N times in Bash
Why are Latin and Sanskrit called dead languages?
When making yogurt, why doesn't bad bacteria grow as well?
Why does printf behave this way when using struct?
When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Why are mutable structs “evil”?When to use struct?Why does printf not flush after the call unless a newline is in the format string?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why Choose Struct Over Class?Why does the error struct FILE* but argument is of type char* appear?in a struct, scanf returns var address not valueSegmentation Fault when calling member of a struct
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am writing a code while making the use of structures. I am new to structs so I am practicing to get used to it. Anyway while trying to use printf on a variable of type string which is a variable of a struct type, printf only prints '@' instead of the whole string.
...
void signPlayers(struct player players[9], int playersC) // players is an array declared on main and playersC is the size of it.
for (int i = 0; i < playersC; i++)
char tname[22];
printf("Please enter the name of the player #%d: n",i+1);
int res = scanf(" %s",&tname);
while(res != 1)
printf("Please enter a valid name for player #%d: n",i+1);
res = scanf(" %s",&tname);
players[i].name = tname;
printf("Player #%d signed as %s!nn",players[i].id,players[i].name); // this printf actually works fine
int checkForWinner(struct player players[], int playersC)
for (int i = 0; i < playersC; i++)
if (players[i].pos == 10)
return 0;
printf("%sn",players[i].name); // prints "@" instead of the name
return 1;
...
so If I entered the name Joey, At first printf it actually prints "Joey", then when I call the checkForWinner function (Its called after signPlayers function), the printf now prints only "@" instead of the whole name again.
What could be wrong?
c struct
|
show 5 more comments
I am writing a code while making the use of structures. I am new to structs so I am practicing to get used to it. Anyway while trying to use printf on a variable of type string which is a variable of a struct type, printf only prints '@' instead of the whole string.
...
void signPlayers(struct player players[9], int playersC) // players is an array declared on main and playersC is the size of it.
for (int i = 0; i < playersC; i++)
char tname[22];
printf("Please enter the name of the player #%d: n",i+1);
int res = scanf(" %s",&tname);
while(res != 1)
printf("Please enter a valid name for player #%d: n",i+1);
res = scanf(" %s",&tname);
players[i].name = tname;
printf("Player #%d signed as %s!nn",players[i].id,players[i].name); // this printf actually works fine
int checkForWinner(struct player players[], int playersC)
for (int i = 0; i < playersC; i++)
if (players[i].pos == 10)
return 0;
printf("%sn",players[i].name); // prints "@" instead of the name
return 1;
...
so If I entered the name Joey, At first printf it actually prints "Joey", then when I call the checkForWinner function (Its called after signPlayers function), the printf now prints only "@" instead of the whole name again.
What could be wrong?
c struct
3
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
2
@AmeerShehadey The assignmentplayers[i].name = tname
is assigning the address of achar
array that exists only on the stack, and only for the duration of the call tosignPlayers
. You're then attempting to print the contents of that array, buttname
went out of scope when the call tosignPlayers
completed, and now the memory thatplayers[i].name
is pointing to is just whatever random garbage was left over afterwards.
– Mike Holt
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Alsoscanf(" %s",&tname);
-->scanf("%21s",tname);
– chux
Mar 28 at 2:20
|
show 5 more comments
I am writing a code while making the use of structures. I am new to structs so I am practicing to get used to it. Anyway while trying to use printf on a variable of type string which is a variable of a struct type, printf only prints '@' instead of the whole string.
...
void signPlayers(struct player players[9], int playersC) // players is an array declared on main and playersC is the size of it.
for (int i = 0; i < playersC; i++)
char tname[22];
printf("Please enter the name of the player #%d: n",i+1);
int res = scanf(" %s",&tname);
while(res != 1)
printf("Please enter a valid name for player #%d: n",i+1);
res = scanf(" %s",&tname);
players[i].name = tname;
printf("Player #%d signed as %s!nn",players[i].id,players[i].name); // this printf actually works fine
int checkForWinner(struct player players[], int playersC)
for (int i = 0; i < playersC; i++)
if (players[i].pos == 10)
return 0;
printf("%sn",players[i].name); // prints "@" instead of the name
return 1;
...
so If I entered the name Joey, At first printf it actually prints "Joey", then when I call the checkForWinner function (Its called after signPlayers function), the printf now prints only "@" instead of the whole name again.
What could be wrong?
c struct
I am writing a code while making the use of structures. I am new to structs so I am practicing to get used to it. Anyway while trying to use printf on a variable of type string which is a variable of a struct type, printf only prints '@' instead of the whole string.
...
void signPlayers(struct player players[9], int playersC) // players is an array declared on main and playersC is the size of it.
for (int i = 0; i < playersC; i++)
char tname[22];
printf("Please enter the name of the player #%d: n",i+1);
int res = scanf(" %s",&tname);
while(res != 1)
printf("Please enter a valid name for player #%d: n",i+1);
res = scanf(" %s",&tname);
players[i].name = tname;
printf("Player #%d signed as %s!nn",players[i].id,players[i].name); // this printf actually works fine
int checkForWinner(struct player players[], int playersC)
for (int i = 0; i < playersC; i++)
if (players[i].pos == 10)
return 0;
printf("%sn",players[i].name); // prints "@" instead of the name
return 1;
...
so If I entered the name Joey, At first printf it actually prints "Joey", then when I call the checkForWinner function (Its called after signPlayers function), the printf now prints only "@" instead of the whole name again.
What could be wrong?
c struct
c struct
asked Mar 28 at 1:46
Ameer ShehadeyAmeer Shehadey
83 bronze badges
83 bronze badges
3
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
2
@AmeerShehadey The assignmentplayers[i].name = tname
is assigning the address of achar
array that exists only on the stack, and only for the duration of the call tosignPlayers
. You're then attempting to print the contents of that array, buttname
went out of scope when the call tosignPlayers
completed, and now the memory thatplayers[i].name
is pointing to is just whatever random garbage was left over afterwards.
– Mike Holt
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Alsoscanf(" %s",&tname);
-->scanf("%21s",tname);
– chux
Mar 28 at 2:20
|
show 5 more comments
3
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
2
@AmeerShehadey The assignmentplayers[i].name = tname
is assigning the address of achar
array that exists only on the stack, and only for the duration of the call tosignPlayers
. You're then attempting to print the contents of that array, buttname
went out of scope when the call tosignPlayers
completed, and now the memory thatplayers[i].name
is pointing to is just whatever random garbage was left over afterwards.
– Mike Holt
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Alsoscanf(" %s",&tname);
-->scanf("%21s",tname);
– chux
Mar 28 at 2:20
3
3
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
2
2
@AmeerShehadey The assignment
players[i].name = tname
is assigning the address of a char
array that exists only on the stack, and only for the duration of the call to signPlayers
. You're then attempting to print the contents of that array, but tname
went out of scope when the call to signPlayers
completed, and now the memory that players[i].name
is pointing to is just whatever random garbage was left over afterwards.– Mike Holt
Mar 28 at 2:19
@AmeerShehadey The assignment
players[i].name = tname
is assigning the address of a char
array that exists only on the stack, and only for the duration of the call to signPlayers
. You're then attempting to print the contents of that array, but tname
went out of scope when the call to signPlayers
completed, and now the memory that players[i].name
is pointing to is just whatever random garbage was left over afterwards.– Mike Holt
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Also
scanf(" %s",&tname);
--> scanf("%21s",tname);
– chux
Mar 28 at 2:20
Also
scanf(" %s",&tname);
--> scanf("%21s",tname);
– chux
Mar 28 at 2:20
|
show 5 more comments
1 Answer
1
active
oldest
votes
Your code is attempting to access stack memory after the function has returned. When you do that, you get just whatever garbage was left over after the function call, or perhaps some part of a new stack frame belonging to a different function. Either way, it's undefined behavior.
There are several problems with the assignment players[i].name = tname;
in function signPlayers
:
It is assigning the address of the local array variable
tname
. This variable goes out of scope on every iteration through the loop. That means accessing that pointer after the loop will be undefined behavior.Even if
tname
was moved out of the loop's scope up to the function scope, it will still go out of scope as soon as the function returns. Either way, accessing it after the function call is undefined behavior.It's likely that the address of
tname
doesn't change from one iteration of the loop to the next. So that means that the.name
member of every player in theplayers
array will probably be identical (as well as being invalid).
There are many ways to fix this. Here are three ways:
Make a copy of
tname
each time through the loop usingstrdup(tname)
, and assign that toplayers[i].name
:players[i].name = strdup(tname);
The
strdup
function allocates memory, so if you use this approach, you will need to remember tofree
the.name
member of each player when you're done.Allocate memory dynamically to each
players[i].name
prior to callingsignPlayers
:for (i=0; i<playersC; ++i) players[i].name = malloc(22);
signPlayers(players, playersC);
// Don't forget to call free() on each .name member after you're doneInside
signPlayers
, you would then get rid oftname
altogether and do:int res = scanf(" %s", players[i].name);
Note: No need to do
22 * sizeof(char)
here, since the C standard guaranteessizeof(char) == 1
. Also, I used 22 because that's what OP's code uses to declaretname
. However, it should be noted thatscanf
is less than ideal here, because it gives no way to limit the number of bytes written to the array, and thus no way to protect against buffer overflow. If you type a name longer than 21 characters (need to leave 1 byte for the null terminator), then you will overflowtname
and your program will either crash or silently corrupt your data.Turn
players[i].name
into a sizedchar
array instead of just achar
pointer. In other words, statically allocate memory to eachplayers[i].name
. This has the advantage of not needing to callfree
, as you would need to do with methods 1 and 2. OP didn't show the definition ofstruct player
, but this should suffice for example purposes:struct player
char name[22];
// other stuff
;And again, inside
signPlayers
, you wouldscanf
directly intoplayers[i].name
instead of usingtname
.
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/3.0/"u003ecc by-sa 3.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%2f55388988%2fwhy-does-printf-behave-this-way-when-using-struct%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code is attempting to access stack memory after the function has returned. When you do that, you get just whatever garbage was left over after the function call, or perhaps some part of a new stack frame belonging to a different function. Either way, it's undefined behavior.
There are several problems with the assignment players[i].name = tname;
in function signPlayers
:
It is assigning the address of the local array variable
tname
. This variable goes out of scope on every iteration through the loop. That means accessing that pointer after the loop will be undefined behavior.Even if
tname
was moved out of the loop's scope up to the function scope, it will still go out of scope as soon as the function returns. Either way, accessing it after the function call is undefined behavior.It's likely that the address of
tname
doesn't change from one iteration of the loop to the next. So that means that the.name
member of every player in theplayers
array will probably be identical (as well as being invalid).
There are many ways to fix this. Here are three ways:
Make a copy of
tname
each time through the loop usingstrdup(tname)
, and assign that toplayers[i].name
:players[i].name = strdup(tname);
The
strdup
function allocates memory, so if you use this approach, you will need to remember tofree
the.name
member of each player when you're done.Allocate memory dynamically to each
players[i].name
prior to callingsignPlayers
:for (i=0; i<playersC; ++i) players[i].name = malloc(22);
signPlayers(players, playersC);
// Don't forget to call free() on each .name member after you're doneInside
signPlayers
, you would then get rid oftname
altogether and do:int res = scanf(" %s", players[i].name);
Note: No need to do
22 * sizeof(char)
here, since the C standard guaranteessizeof(char) == 1
. Also, I used 22 because that's what OP's code uses to declaretname
. However, it should be noted thatscanf
is less than ideal here, because it gives no way to limit the number of bytes written to the array, and thus no way to protect against buffer overflow. If you type a name longer than 21 characters (need to leave 1 byte for the null terminator), then you will overflowtname
and your program will either crash or silently corrupt your data.Turn
players[i].name
into a sizedchar
array instead of just achar
pointer. In other words, statically allocate memory to eachplayers[i].name
. This has the advantage of not needing to callfree
, as you would need to do with methods 1 and 2. OP didn't show the definition ofstruct player
, but this should suffice for example purposes:struct player
char name[22];
// other stuff
;And again, inside
signPlayers
, you wouldscanf
directly intoplayers[i].name
instead of usingtname
.
add a comment |
Your code is attempting to access stack memory after the function has returned. When you do that, you get just whatever garbage was left over after the function call, or perhaps some part of a new stack frame belonging to a different function. Either way, it's undefined behavior.
There are several problems with the assignment players[i].name = tname;
in function signPlayers
:
It is assigning the address of the local array variable
tname
. This variable goes out of scope on every iteration through the loop. That means accessing that pointer after the loop will be undefined behavior.Even if
tname
was moved out of the loop's scope up to the function scope, it will still go out of scope as soon as the function returns. Either way, accessing it after the function call is undefined behavior.It's likely that the address of
tname
doesn't change from one iteration of the loop to the next. So that means that the.name
member of every player in theplayers
array will probably be identical (as well as being invalid).
There are many ways to fix this. Here are three ways:
Make a copy of
tname
each time through the loop usingstrdup(tname)
, and assign that toplayers[i].name
:players[i].name = strdup(tname);
The
strdup
function allocates memory, so if you use this approach, you will need to remember tofree
the.name
member of each player when you're done.Allocate memory dynamically to each
players[i].name
prior to callingsignPlayers
:for (i=0; i<playersC; ++i) players[i].name = malloc(22);
signPlayers(players, playersC);
// Don't forget to call free() on each .name member after you're doneInside
signPlayers
, you would then get rid oftname
altogether and do:int res = scanf(" %s", players[i].name);
Note: No need to do
22 * sizeof(char)
here, since the C standard guaranteessizeof(char) == 1
. Also, I used 22 because that's what OP's code uses to declaretname
. However, it should be noted thatscanf
is less than ideal here, because it gives no way to limit the number of bytes written to the array, and thus no way to protect against buffer overflow. If you type a name longer than 21 characters (need to leave 1 byte for the null terminator), then you will overflowtname
and your program will either crash or silently corrupt your data.Turn
players[i].name
into a sizedchar
array instead of just achar
pointer. In other words, statically allocate memory to eachplayers[i].name
. This has the advantage of not needing to callfree
, as you would need to do with methods 1 and 2. OP didn't show the definition ofstruct player
, but this should suffice for example purposes:struct player
char name[22];
// other stuff
;And again, inside
signPlayers
, you wouldscanf
directly intoplayers[i].name
instead of usingtname
.
add a comment |
Your code is attempting to access stack memory after the function has returned. When you do that, you get just whatever garbage was left over after the function call, or perhaps some part of a new stack frame belonging to a different function. Either way, it's undefined behavior.
There are several problems with the assignment players[i].name = tname;
in function signPlayers
:
It is assigning the address of the local array variable
tname
. This variable goes out of scope on every iteration through the loop. That means accessing that pointer after the loop will be undefined behavior.Even if
tname
was moved out of the loop's scope up to the function scope, it will still go out of scope as soon as the function returns. Either way, accessing it after the function call is undefined behavior.It's likely that the address of
tname
doesn't change from one iteration of the loop to the next. So that means that the.name
member of every player in theplayers
array will probably be identical (as well as being invalid).
There are many ways to fix this. Here are three ways:
Make a copy of
tname
each time through the loop usingstrdup(tname)
, and assign that toplayers[i].name
:players[i].name = strdup(tname);
The
strdup
function allocates memory, so if you use this approach, you will need to remember tofree
the.name
member of each player when you're done.Allocate memory dynamically to each
players[i].name
prior to callingsignPlayers
:for (i=0; i<playersC; ++i) players[i].name = malloc(22);
signPlayers(players, playersC);
// Don't forget to call free() on each .name member after you're doneInside
signPlayers
, you would then get rid oftname
altogether and do:int res = scanf(" %s", players[i].name);
Note: No need to do
22 * sizeof(char)
here, since the C standard guaranteessizeof(char) == 1
. Also, I used 22 because that's what OP's code uses to declaretname
. However, it should be noted thatscanf
is less than ideal here, because it gives no way to limit the number of bytes written to the array, and thus no way to protect against buffer overflow. If you type a name longer than 21 characters (need to leave 1 byte for the null terminator), then you will overflowtname
and your program will either crash or silently corrupt your data.Turn
players[i].name
into a sizedchar
array instead of just achar
pointer. In other words, statically allocate memory to eachplayers[i].name
. This has the advantage of not needing to callfree
, as you would need to do with methods 1 and 2. OP didn't show the definition ofstruct player
, but this should suffice for example purposes:struct player
char name[22];
// other stuff
;And again, inside
signPlayers
, you wouldscanf
directly intoplayers[i].name
instead of usingtname
.
Your code is attempting to access stack memory after the function has returned. When you do that, you get just whatever garbage was left over after the function call, or perhaps some part of a new stack frame belonging to a different function. Either way, it's undefined behavior.
There are several problems with the assignment players[i].name = tname;
in function signPlayers
:
It is assigning the address of the local array variable
tname
. This variable goes out of scope on every iteration through the loop. That means accessing that pointer after the loop will be undefined behavior.Even if
tname
was moved out of the loop's scope up to the function scope, it will still go out of scope as soon as the function returns. Either way, accessing it after the function call is undefined behavior.It's likely that the address of
tname
doesn't change from one iteration of the loop to the next. So that means that the.name
member of every player in theplayers
array will probably be identical (as well as being invalid).
There are many ways to fix this. Here are three ways:
Make a copy of
tname
each time through the loop usingstrdup(tname)
, and assign that toplayers[i].name
:players[i].name = strdup(tname);
The
strdup
function allocates memory, so if you use this approach, you will need to remember tofree
the.name
member of each player when you're done.Allocate memory dynamically to each
players[i].name
prior to callingsignPlayers
:for (i=0; i<playersC; ++i) players[i].name = malloc(22);
signPlayers(players, playersC);
// Don't forget to call free() on each .name member after you're doneInside
signPlayers
, you would then get rid oftname
altogether and do:int res = scanf(" %s", players[i].name);
Note: No need to do
22 * sizeof(char)
here, since the C standard guaranteessizeof(char) == 1
. Also, I used 22 because that's what OP's code uses to declaretname
. However, it should be noted thatscanf
is less than ideal here, because it gives no way to limit the number of bytes written to the array, and thus no way to protect against buffer overflow. If you type a name longer than 21 characters (need to leave 1 byte for the null terminator), then you will overflowtname
and your program will either crash or silently corrupt your data.Turn
players[i].name
into a sizedchar
array instead of just achar
pointer. In other words, statically allocate memory to eachplayers[i].name
. This has the advantage of not needing to callfree
, as you would need to do with methods 1 and 2. OP didn't show the definition ofstruct player
, but this should suffice for example purposes:struct player
char name[22];
// other stuff
;And again, inside
signPlayers
, you wouldscanf
directly intoplayers[i].name
instead of usingtname
.
edited Mar 28 at 8:21
answered Mar 28 at 8:16
Mike HoltMike Holt
3,5891 gold badge10 silver badges22 bronze badges
3,5891 gold badge10 silver badges22 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55388988%2fwhy-does-printf-behave-this-way-when-using-struct%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
3
Your program has undefined behavior because you store a pointer to memory on the stack and then try to use it after that stack has gone out of scope.
– paddy
Mar 28 at 1:50
Wait I didn't get it, so what pointer did you talk about and when or why did the stack go out of scope? If I may ask. @paddy
– Ameer Shehadey
Mar 28 at 1:56
2
@AmeerShehadey The assignment
players[i].name = tname
is assigning the address of achar
array that exists only on the stack, and only for the duration of the call tosignPlayers
. You're then attempting to print the contents of that array, buttname
went out of scope when the call tosignPlayers
completed, and now the memory thatplayers[i].name
is pointing to is just whatever random garbage was left over afterwards.– Mike Holt
Mar 28 at 2:19
Okay, I think problem is solved after using the name member on the player struct as char name[] instead of char* name.
– Ameer Shehadey
Mar 28 at 2:19
Also
scanf(" %s",&tname);
-->scanf("%21s",tname);
– chux
Mar 28 at 2:20