When accessing my graphic in my struct it prints an empty line
Here is the struct that I am using.
#define MAX_CAR_LEN 12 ///< length does not include the trailing NUL byte
/// Racer_S structure represents a racer's row, position and display graphic.
typedef struct Racer_S {
int row; ///< vertical row or "racing lane" of a racer
int distance; ///< column of rear of car, marking its position in race
char *graphic; ///< graphic is the drawable text of the racer figure
} Racer;
When I call this function everything works fine inside it and creates everything correctly. I am able to access the row and distance fine. When I try and print the graphic I am printed an empty row in my terminal. I believe that this might be because "graphic" in the struct is a char* but I assign it a fixed sized array of char. When this function is called and passed in the name "Tom", graphic is supposed to be "~O=Tom----o>". I am new to C what am I doing wrong?
Racer * make_racer( char *name, int row ){
//Creating a newRacer instance
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN);
char car[MAX_CAR_LEN] = "~O="; //"~O=-------o>"
char *pCar;
int length = strlen(name) + 2;
//Add the name after the engine of the car
for (int i = 0; i < length; ++i)
car[i+3] = name[i];
//Calculate the amount of dashes needed
int printDashes = 7 - strlen(name);
//add the extra dashes
for (int j = 1; j <= printDashes; ++j)
car[length + j] = '-';
// creates the end of the car
car[MAX_CAR_LEN-2] = 'o';
car[MAX_CAR_LEN-1] = '>';
pCar = strdup(car);
newRacer->row = row;
newRacer->distance = 0;
newRacer->graphic = &car[0];
// printf("%sn", car);
return newRacer;
}
This is the code I am running in my main to test it
Racer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
c string pointers char c99
|
show 1 more comment
Here is the struct that I am using.
#define MAX_CAR_LEN 12 ///< length does not include the trailing NUL byte
/// Racer_S structure represents a racer's row, position and display graphic.
typedef struct Racer_S {
int row; ///< vertical row or "racing lane" of a racer
int distance; ///< column of rear of car, marking its position in race
char *graphic; ///< graphic is the drawable text of the racer figure
} Racer;
When I call this function everything works fine inside it and creates everything correctly. I am able to access the row and distance fine. When I try and print the graphic I am printed an empty row in my terminal. I believe that this might be because "graphic" in the struct is a char* but I assign it a fixed sized array of char. When this function is called and passed in the name "Tom", graphic is supposed to be "~O=Tom----o>". I am new to C what am I doing wrong?
Racer * make_racer( char *name, int row ){
//Creating a newRacer instance
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN);
char car[MAX_CAR_LEN] = "~O="; //"~O=-------o>"
char *pCar;
int length = strlen(name) + 2;
//Add the name after the engine of the car
for (int i = 0; i < length; ++i)
car[i+3] = name[i];
//Calculate the amount of dashes needed
int printDashes = 7 - strlen(name);
//add the extra dashes
for (int j = 1; j <= printDashes; ++j)
car[length + j] = '-';
// creates the end of the car
car[MAX_CAR_LEN-2] = 'o';
car[MAX_CAR_LEN-1] = '>';
pCar = strdup(car);
newRacer->row = row;
newRacer->distance = 0;
newRacer->graphic = &car[0];
// printf("%sn", car);
return newRacer;
}
This is the code I am running in my main to test it
Racer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
c string pointers char c99
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably usestrdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.
– Retired Ninja
Nov 14 '18 at 2:49
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
It has been updated
– Jak
Nov 14 '18 at 4:37
|
show 1 more comment
Here is the struct that I am using.
#define MAX_CAR_LEN 12 ///< length does not include the trailing NUL byte
/// Racer_S structure represents a racer's row, position and display graphic.
typedef struct Racer_S {
int row; ///< vertical row or "racing lane" of a racer
int distance; ///< column of rear of car, marking its position in race
char *graphic; ///< graphic is the drawable text of the racer figure
} Racer;
When I call this function everything works fine inside it and creates everything correctly. I am able to access the row and distance fine. When I try and print the graphic I am printed an empty row in my terminal. I believe that this might be because "graphic" in the struct is a char* but I assign it a fixed sized array of char. When this function is called and passed in the name "Tom", graphic is supposed to be "~O=Tom----o>". I am new to C what am I doing wrong?
Racer * make_racer( char *name, int row ){
//Creating a newRacer instance
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN);
char car[MAX_CAR_LEN] = "~O="; //"~O=-------o>"
char *pCar;
int length = strlen(name) + 2;
//Add the name after the engine of the car
for (int i = 0; i < length; ++i)
car[i+3] = name[i];
//Calculate the amount of dashes needed
int printDashes = 7 - strlen(name);
//add the extra dashes
for (int j = 1; j <= printDashes; ++j)
car[length + j] = '-';
// creates the end of the car
car[MAX_CAR_LEN-2] = 'o';
car[MAX_CAR_LEN-1] = '>';
pCar = strdup(car);
newRacer->row = row;
newRacer->distance = 0;
newRacer->graphic = &car[0];
// printf("%sn", car);
return newRacer;
}
This is the code I am running in my main to test it
Racer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
c string pointers char c99
Here is the struct that I am using.
#define MAX_CAR_LEN 12 ///< length does not include the trailing NUL byte
/// Racer_S structure represents a racer's row, position and display graphic.
typedef struct Racer_S {
int row; ///< vertical row or "racing lane" of a racer
int distance; ///< column of rear of car, marking its position in race
char *graphic; ///< graphic is the drawable text of the racer figure
} Racer;
When I call this function everything works fine inside it and creates everything correctly. I am able to access the row and distance fine. When I try and print the graphic I am printed an empty row in my terminal. I believe that this might be because "graphic" in the struct is a char* but I assign it a fixed sized array of char. When this function is called and passed in the name "Tom", graphic is supposed to be "~O=Tom----o>". I am new to C what am I doing wrong?
Racer * make_racer( char *name, int row ){
//Creating a newRacer instance
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN);
char car[MAX_CAR_LEN] = "~O="; //"~O=-------o>"
char *pCar;
int length = strlen(name) + 2;
//Add the name after the engine of the car
for (int i = 0; i < length; ++i)
car[i+3] = name[i];
//Calculate the amount of dashes needed
int printDashes = 7 - strlen(name);
//add the extra dashes
for (int j = 1; j <= printDashes; ++j)
car[length + j] = '-';
// creates the end of the car
car[MAX_CAR_LEN-2] = 'o';
car[MAX_CAR_LEN-1] = '>';
pCar = strdup(car);
newRacer->row = row;
newRacer->distance = 0;
newRacer->graphic = &car[0];
// printf("%sn", car);
return newRacer;
}
This is the code I am running in my main to test it
Racer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
c string pointers char c99
c string pointers char c99
edited Nov 14 '18 at 18:00
asked Nov 14 '18 at 2:44
Jak
25
25
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably usestrdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.
– Retired Ninja
Nov 14 '18 at 2:49
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
It has been updated
– Jak
Nov 14 '18 at 4:37
|
show 1 more comment
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably usestrdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.
– Retired Ninja
Nov 14 '18 at 2:49
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
It has been updated
– Jak
Nov 14 '18 at 4:37
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably use
strdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.– Retired Ninja
Nov 14 '18 at 2:49
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably use
strdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.– Retired Ninja
Nov 14 '18 at 2:49
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
It has been updated
– Jak
Nov 14 '18 at 4:37
It has been updated
– Jak
Nov 14 '18 at 4:37
|
show 1 more comment
1 Answer
1
active
oldest
votes
You have mentioned the below statement in your question.
This is the code I am running in my main to test it
acer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
In make_racer function you have used a local charecter array variable called car and assigned the address to newRacer->graphic . This variable(char car[MAX_CAR_LEN+1] ;) memory goes out of scope after returning from the function.
Please refer this thread to understand more about the local scope in C.
Now to resolve your problem, In make_racer function you have to dynamically allocate memory for newRacer->graphic as well.
Racer * make_racer( char *name, int row ){
:
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN+1);
:
//newRacer->graphic = &car[0]; # this is wrong.
strcpy(newRacer->graphic,car); //copy the content to allocated memory.
/*
* Warning:Just i am pointing out the issue here.
* strcpy may cause buffer over run.
* You have to use snprintf/strncpy family to prevent buffer overrun.
*/
}
Make sure you free the memory in your main.
int main() {
:
free(newRacer->graphic); //before free the momory for "newRacer"
free(newRacer);
}
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem withcar
beforestrdup
is called. AlsonewRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local tomake_racer()
which then goes out of scope on the function return. Seems like it should benewRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
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%2f53292447%2fwhen-accessing-my-graphic-in-my-struct-it-prints-an-empty-line%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
You have mentioned the below statement in your question.
This is the code I am running in my main to test it
acer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
In make_racer function you have used a local charecter array variable called car and assigned the address to newRacer->graphic . This variable(char car[MAX_CAR_LEN+1] ;) memory goes out of scope after returning from the function.
Please refer this thread to understand more about the local scope in C.
Now to resolve your problem, In make_racer function you have to dynamically allocate memory for newRacer->graphic as well.
Racer * make_racer( char *name, int row ){
:
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN+1);
:
//newRacer->graphic = &car[0]; # this is wrong.
strcpy(newRacer->graphic,car); //copy the content to allocated memory.
/*
* Warning:Just i am pointing out the issue here.
* strcpy may cause buffer over run.
* You have to use snprintf/strncpy family to prevent buffer overrun.
*/
}
Make sure you free the memory in your main.
int main() {
:
free(newRacer->graphic); //before free the momory for "newRacer"
free(newRacer);
}
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem withcar
beforestrdup
is called. AlsonewRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local tomake_racer()
which then goes out of scope on the function return. Seems like it should benewRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
add a comment |
You have mentioned the below statement in your question.
This is the code I am running in my main to test it
acer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
In make_racer function you have used a local charecter array variable called car and assigned the address to newRacer->graphic . This variable(char car[MAX_CAR_LEN+1] ;) memory goes out of scope after returning from the function.
Please refer this thread to understand more about the local scope in C.
Now to resolve your problem, In make_racer function you have to dynamically allocate memory for newRacer->graphic as well.
Racer * make_racer( char *name, int row ){
:
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN+1);
:
//newRacer->graphic = &car[0]; # this is wrong.
strcpy(newRacer->graphic,car); //copy the content to allocated memory.
/*
* Warning:Just i am pointing out the issue here.
* strcpy may cause buffer over run.
* You have to use snprintf/strncpy family to prevent buffer overrun.
*/
}
Make sure you free the memory in your main.
int main() {
:
free(newRacer->graphic); //before free the momory for "newRacer"
free(newRacer);
}
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem withcar
beforestrdup
is called. AlsonewRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local tomake_racer()
which then goes out of scope on the function return. Seems like it should benewRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
add a comment |
You have mentioned the below statement in your question.
This is the code I am running in my main to test it
acer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
In make_racer function you have used a local charecter array variable called car and assigned the address to newRacer->graphic . This variable(char car[MAX_CAR_LEN+1] ;) memory goes out of scope after returning from the function.
Please refer this thread to understand more about the local scope in C.
Now to resolve your problem, In make_racer function you have to dynamically allocate memory for newRacer->graphic as well.
Racer * make_racer( char *name, int row ){
:
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN+1);
:
//newRacer->graphic = &car[0]; # this is wrong.
strcpy(newRacer->graphic,car); //copy the content to allocated memory.
/*
* Warning:Just i am pointing out the issue here.
* strcpy may cause buffer over run.
* You have to use snprintf/strncpy family to prevent buffer overrun.
*/
}
Make sure you free the memory in your main.
int main() {
:
free(newRacer->graphic); //before free the momory for "newRacer"
free(newRacer);
}
You have mentioned the below statement in your question.
This is the code I am running in my main to test it
acer *t = make_racer("Tom", 4);
printf("%sn", t->graphic);
In make_racer function you have used a local charecter array variable called car and assigned the address to newRacer->graphic . This variable(char car[MAX_CAR_LEN+1] ;) memory goes out of scope after returning from the function.
Please refer this thread to understand more about the local scope in C.
Now to resolve your problem, In make_racer function you have to dynamically allocate memory for newRacer->graphic as well.
Racer * make_racer( char *name, int row ){
:
Racer *newRacer = malloc(sizeof(*newRacer));
newRacer->graphic = (char*)malloc(MAX_CAR_LEN+1);
:
//newRacer->graphic = &car[0]; # this is wrong.
strcpy(newRacer->graphic,car); //copy the content to allocated memory.
/*
* Warning:Just i am pointing out the issue here.
* strcpy may cause buffer over run.
* You have to use snprintf/strncpy family to prevent buffer overrun.
*/
}
Make sure you free the memory in your main.
int main() {
:
free(newRacer->graphic); //before free the momory for "newRacer"
free(newRacer);
}
edited Nov 16 '18 at 17:07
answered Nov 14 '18 at 5:17
Naveen Kumar
494320
494320
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem withcar
beforestrdup
is called. AlsonewRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local tomake_racer()
which then goes out of scope on the function return. Seems like it should benewRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
add a comment |
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem withcar
beforestrdup
is called. AlsonewRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local tomake_racer()
which then goes out of scope on the function return. Seems like it should benewRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I am still getting the error even with the fixes :/
– Jak
Nov 14 '18 at 17:58
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
I have updated my code above for your reference. I would really appreciate it if you could check it over
– Jak
Nov 14 '18 at 18:01
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@Jak, you have to copy the content to allocated memory. memory allocated for "car" will be freed up. please check my updated answer.
– Naveen Kumar
Nov 16 '18 at 16:54
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem with
car
before strdup
is called. Also newRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local to make_racer()
which then goes out of scope on the function return. Seems like it should be newRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
@NaveenKumar - have a look at my comment under the question and see if you can fix the problem with
car
before strdup
is called. Also newRacer->graphic = &car[0];
is wrong because it assigns the address of an array created local to make_racer()
which then goes out of scope on the function return. Seems like it should be newRacer->graphic = pCar;
– David C. Rankin
Nov 16 '18 at 17:10
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53292447%2fwhen-accessing-my-graphic-in-my-struct-it-prints-an-empty-line%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
You need to allocate memory for the string and copy it into that allocated memory. You're just pointing to a variable that immediately goes out of scope. You can probably use
strdup
or do it manually. I will say there's a lot of assumptions being made in your code about lengths. It would be very easy to access outside the bounds of the memory you've allocated.– Retired Ninja
Nov 14 '18 at 2:49
I added a new variable char *pCar = strdup(car); and made newRacer->graphic = pCar... but I am still seg faulting. hmmmmm
– Jak
Nov 14 '18 at 3:04
Consider a Minimal, Complete, and Verifiable example because the problem could very likely be in the code you have not shown, and we'd be able to see things like what MAX_CAR_LEN is.
– Retired Ninja
Nov 14 '18 at 3:07
Okay I will update it now. Sorry about that
– Jak
Nov 14 '18 at 3:09
It has been updated
– Jak
Nov 14 '18 at 4:37