How to iterate through a char* in C99? [duplicate]










-2















This question already has an answer here:



  • What is the difference between char s and char *s?

    12 answers



  • Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?

    16 answers



I have tried a few different things to get this to work but basically I have a name that is 6 letters long (maximum) and I need to input it into the char* car in place of the '-'. For example if the name was Bob the car should look like ~O=Bob----o>



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;

Racer * make_racer( char *name, int row )
Racer *newRacer;
char *car = "~O=-------o>";
for (int i = 0; i < strlen(name); ++i)
car[i+3] = name[i];
// printf("%sn",car);
// newRacer->row = row;
// newRacer->distance = 0;
// newRacer->graphic = car;
return newRacer;










share|improve this question















marked as duplicate by Mad Physicist, Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 11:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What exactly is Racer? Is it something you have control over?
    – Mad Physicist
    Nov 13 '18 at 3:03






  • 1




    On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
    – Mad Physicist
    Nov 13 '18 at 3:04










  • geeksforgeeks.org/strstr-in-ccpp
    – Bwebb
    Nov 13 '18 at 3:05










  • Is Racer a pointer to a char *? Please provide the definiton
    – Hogstrom
    Nov 13 '18 at 3:09










  • Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
    – ShadowRanger
    Nov 13 '18 at 4:05















-2















This question already has an answer here:



  • What is the difference between char s and char *s?

    12 answers



  • Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?

    16 answers



I have tried a few different things to get this to work but basically I have a name that is 6 letters long (maximum) and I need to input it into the char* car in place of the '-'. For example if the name was Bob the car should look like ~O=Bob----o>



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;

Racer * make_racer( char *name, int row )
Racer *newRacer;
char *car = "~O=-------o>";
for (int i = 0; i < strlen(name); ++i)
car[i+3] = name[i];
// printf("%sn",car);
// newRacer->row = row;
// newRacer->distance = 0;
// newRacer->graphic = car;
return newRacer;










share|improve this question















marked as duplicate by Mad Physicist, Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 11:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What exactly is Racer? Is it something you have control over?
    – Mad Physicist
    Nov 13 '18 at 3:03






  • 1




    On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
    – Mad Physicist
    Nov 13 '18 at 3:04










  • geeksforgeeks.org/strstr-in-ccpp
    – Bwebb
    Nov 13 '18 at 3:05










  • Is Racer a pointer to a char *? Please provide the definiton
    – Hogstrom
    Nov 13 '18 at 3:09










  • Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
    – ShadowRanger
    Nov 13 '18 at 4:05













-2












-2








-2








This question already has an answer here:



  • What is the difference between char s and char *s?

    12 answers



  • Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?

    16 answers



I have tried a few different things to get this to work but basically I have a name that is 6 letters long (maximum) and I need to input it into the char* car in place of the '-'. For example if the name was Bob the car should look like ~O=Bob----o>



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;

Racer * make_racer( char *name, int row )
Racer *newRacer;
char *car = "~O=-------o>";
for (int i = 0; i < strlen(name); ++i)
car[i+3] = name[i];
// printf("%sn",car);
// newRacer->row = row;
// newRacer->distance = 0;
// newRacer->graphic = car;
return newRacer;










share|improve this question
















This question already has an answer here:



  • What is the difference between char s and char *s?

    12 answers



  • Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?

    16 answers



I have tried a few different things to get this to work but basically I have a name that is 6 letters long (maximum) and I need to input it into the char* car in place of the '-'. For example if the name was Bob the car should look like ~O=Bob----o>



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;

Racer * make_racer( char *name, int row )
Racer *newRacer;
char *car = "~O=-------o>";
for (int i = 0; i < strlen(name); ++i)
car[i+3] = name[i];
// printf("%sn",car);
// newRacer->row = row;
// newRacer->distance = 0;
// newRacer->graphic = car;
return newRacer;





This question already has an answer here:



  • What is the difference between char s and char *s?

    12 answers



  • Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?

    16 answers







c arrays string c99






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 3:58







Jak

















asked Nov 13 '18 at 3:00









JakJak

25




25




marked as duplicate by Mad Physicist, Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 11:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mad Physicist, Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 11:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • What exactly is Racer? Is it something you have control over?
    – Mad Physicist
    Nov 13 '18 at 3:03






  • 1




    On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
    – Mad Physicist
    Nov 13 '18 at 3:04










  • geeksforgeeks.org/strstr-in-ccpp
    – Bwebb
    Nov 13 '18 at 3:05










  • Is Racer a pointer to a char *? Please provide the definiton
    – Hogstrom
    Nov 13 '18 at 3:09










  • Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
    – ShadowRanger
    Nov 13 '18 at 4:05
















  • What exactly is Racer? Is it something you have control over?
    – Mad Physicist
    Nov 13 '18 at 3:03






  • 1




    On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
    – Mad Physicist
    Nov 13 '18 at 3:04










  • geeksforgeeks.org/strstr-in-ccpp
    – Bwebb
    Nov 13 '18 at 3:05










  • Is Racer a pointer to a char *? Please provide the definiton
    – Hogstrom
    Nov 13 '18 at 3:09










  • Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
    – ShadowRanger
    Nov 13 '18 at 4:05















What exactly is Racer? Is it something you have control over?
– Mad Physicist
Nov 13 '18 at 3:03




What exactly is Racer? Is it something you have control over?
– Mad Physicist
Nov 13 '18 at 3:03




1




1




On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
– Mad Physicist
Nov 13 '18 at 3:04




On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack.
– Mad Physicist
Nov 13 '18 at 3:04












geeksforgeeks.org/strstr-in-ccpp
– Bwebb
Nov 13 '18 at 3:05




geeksforgeeks.org/strstr-in-ccpp
– Bwebb
Nov 13 '18 at 3:05












Is Racer a pointer to a char *? Please provide the definiton
– Hogstrom
Nov 13 '18 at 3:09




Is Racer a pointer to a char *? Please provide the definiton
– Hogstrom
Nov 13 '18 at 3:09












Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
– ShadowRanger
Nov 13 '18 at 4:05




Side-note: Relying on the compiler to compute strlen only once is not a great idea. I'd strongly recommend computing strlen(name) exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially O(n**2) by repeating the strlen test strlen times.
– ShadowRanger
Nov 13 '18 at 4:05












2 Answers
2






active

oldest

votes


















1














car points to a string constant. These are read only, so you can't modify them.



Instead, allocate memory for a string, then modify that:



char *car = strdup("~O=-------o>");


You also need to allocate memory for newRacer:



Racer *newRacer = malloc(sizeof(*newRacer));


Don't forget to check the return value of these functions in case they fail.






share|improve this answer




















  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
    – Lundin
    Nov 13 '18 at 11:48










  • That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
    – Lundin
    Nov 13 '18 at 11:49



















0














The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char, like so:



char car = "~O=-------o>";


This is basically syntactic sugar for the following, equivalent code:



char car = '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '' ;





share|improve this answer


















  • 1




    You should include the trailling NUL character when initializing with braces: ..., '>', ''};
    – Keine Lust
    Nov 13 '18 at 7:47

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














car points to a string constant. These are read only, so you can't modify them.



Instead, allocate memory for a string, then modify that:



char *car = strdup("~O=-------o>");


You also need to allocate memory for newRacer:



Racer *newRacer = malloc(sizeof(*newRacer));


Don't forget to check the return value of these functions in case they fail.






share|improve this answer




















  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
    – Lundin
    Nov 13 '18 at 11:48










  • That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
    – Lundin
    Nov 13 '18 at 11:49
















1














car points to a string constant. These are read only, so you can't modify them.



Instead, allocate memory for a string, then modify that:



char *car = strdup("~O=-------o>");


You also need to allocate memory for newRacer:



Racer *newRacer = malloc(sizeof(*newRacer));


Don't forget to check the return value of these functions in case they fail.






share|improve this answer




















  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
    – Lundin
    Nov 13 '18 at 11:48










  • That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
    – Lundin
    Nov 13 '18 at 11:49














1












1








1






car points to a string constant. These are read only, so you can't modify them.



Instead, allocate memory for a string, then modify that:



char *car = strdup("~O=-------o>");


You also need to allocate memory for newRacer:



Racer *newRacer = malloc(sizeof(*newRacer));


Don't forget to check the return value of these functions in case they fail.






share|improve this answer












car points to a string constant. These are read only, so you can't modify them.



Instead, allocate memory for a string, then modify that:



char *car = strdup("~O=-------o>");


You also need to allocate memory for newRacer:



Racer *newRacer = malloc(sizeof(*newRacer));


Don't forget to check the return value of these functions in case they fail.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 3:08









dbushdbush

93.7k12101134




93.7k12101134











  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
    – Lundin
    Nov 13 '18 at 11:48










  • That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
    – Lundin
    Nov 13 '18 at 11:49

















  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
    – Lundin
    Nov 13 '18 at 11:48










  • That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
    – Lundin
    Nov 13 '18 at 11:49
















This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
– Lundin
Nov 13 '18 at 11:48




This has been answered multiple times before. Instead of posting yet another answer, you should go to the C wiki FAQ, pick a canonical duplicate from the list, section "strings", and close the question.
– Lundin
Nov 13 '18 at 11:48












That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49





That being said, teaching newbies to use strdup without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49














0














The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char, like so:



char car = "~O=-------o>";


This is basically syntactic sugar for the following, equivalent code:



char car = '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '' ;





share|improve this answer


















  • 1




    You should include the trailling NUL character when initializing with braces: ..., '>', ''};
    – Keine Lust
    Nov 13 '18 at 7:47















0














The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char, like so:



char car = "~O=-------o>";


This is basically syntactic sugar for the following, equivalent code:



char car = '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '' ;





share|improve this answer


















  • 1




    You should include the trailling NUL character when initializing with braces: ..., '>', ''};
    – Keine Lust
    Nov 13 '18 at 7:47













0












0








0






The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char, like so:



char car = "~O=-------o>";


This is basically syntactic sugar for the following, equivalent code:



char car = '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '' ;





share|improve this answer














The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char, like so:



char car = "~O=-------o>";


This is basically syntactic sugar for the following, equivalent code:



char car = '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '' ;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 19:25

























answered Nov 13 '18 at 4:10









kamoroso94kamoroso94

1,32511116




1,32511116







  • 1




    You should include the trailling NUL character when initializing with braces: ..., '>', ''};
    – Keine Lust
    Nov 13 '18 at 7:47












  • 1




    You should include the trailling NUL character when initializing with braces: ..., '>', ''};
    – Keine Lust
    Nov 13 '18 at 7:47







1




1




You should include the trailling NUL character when initializing with braces: ..., '>', ''};
– Keine Lust
Nov 13 '18 at 7:47




You should include the trailling NUL character when initializing with braces: ..., '>', ''};
– Keine Lust
Nov 13 '18 at 7:47



這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3