Can't change text in a struct? O.o

Non-platform specific questions.

Can't change text in a struct? O.o

Postby Hblade » Tue Nov 18, 2014 9:50 pm

For some reason game-editor keeps crashing when ever I try to change any text-based values inside a struct. For example:
Code: Select all
typedef struct {
    char*player_name;
               }profile;


I have a reason for using structs, and there's more variables inside the struct I just shortened it for the example. When ever I try to use sprintf, it crashes, when I use strcpy, nothing gets written O.o...

Code: Select all
strcpy(text, player_profile.player_name);

^ This part crashes it, trying to display the text.
Code: Select all
strcpy(player_profile.player_name, "example...");

^ This doesn't do anything O.o... Or if it does, I'm not able to display the output.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Can't change text in a struct? O.o

Postby digiot » Wed Nov 19, 2014 12:01 am

Maybe, in the structure "player_profile", there is no variable "player_name",
or there is no such structure at all ?

Cheers!
User avatar
digiot
 
Posts: 211
Joined: Sat Mar 17, 2012 2:29 pm
Score: 7 Give a positive score

Re: Can't change text in a struct? O.o

Postby skydereign » Wed Nov 19, 2014 12:33 am

That's because the char* isn't pointing to any memory. You would either need to allocate some memory for the char* or just use a char array.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Can't change text in a struct? O.o

Postby Hblade » Wed Nov 19, 2014 4:30 am

Ahh ok thanks. I sort of found out how to do what I was trying to do without using structs though lol so yay O:
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Can't change text in a struct? O.o

Postby bat78 » Thu Jul 30, 2015 1:07 pm

skydereign wrote:That's because the char* isn't pointing to any memory. You would either need to allocate some memory for the char* or just use a char array.

Not quite like so, skydereign.
It crashes exactly, because it DOES point to a memory, to an arbitrary memory address (obviously as the pointer points to a garbage value, since not initialized). Sometimes OS would not allow it, sometimes the memory address will not be accessible/immutable, sometimes it will not actually crash! This is a complete Undefined Behavior.

To make it safe, without causing it to crash, initialize it to 0.
Code: Select all
typedef struct { char* ptr; } mystruct;

Code: Select all
mystruct obj = { NULL };

In that case, this is the so called NULL Pointer and this is what actually literally doesn't point to anywhere. In that case It is very unlikely that will crash, especially on using strcpy, because such functions are designed to either return error or EOF if the pointer given on their argument is NULL and they will be unable to perform the task, therefore, nothing will be printed, copied or modified and the function will simply - fail :)
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron