How to iterate through a char* in C99? [duplicate]
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;
c arrays string c99
marked as duplicate by Mad Physicist, Lundin
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.
|
show 1 more comment
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;
c arrays string c99
marked as duplicate by Mad Physicist, Lundin
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 isRacer
? 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 computestrlen
only once is not a great idea. I'd strongly recommend computingstrlen(name)
exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentiallyO(n**2)
by repeating thestrlen
teststrlen
times.
– ShadowRanger
Nov 13 '18 at 4:05
|
show 1 more comment
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;
c arrays string c99
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
c arrays string c99
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
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
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 isRacer
? 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 computestrlen
only once is not a great idea. I'd strongly recommend computingstrlen(name)
exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentiallyO(n**2)
by repeating thestrlen
teststrlen
times.
– ShadowRanger
Nov 13 '18 at 4:05
|
show 1 more comment
What exactly isRacer
? 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 computestrlen
only once is not a great idea. I'd strongly recommend computingstrlen(name)
exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentiallyO(n**2)
by repeating thestrlen
teststrlen
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
|
show 1 more comment
2 Answers
2
active
oldest
votes
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.
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 usestrdup
without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49
add a comment |
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', '>', '' ;
1
You should include the trailling NUL character when initializing with braces:..., '>', ''};
– Keine Lust
Nov 13 '18 at 7:47
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 usestrdup
without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49
add a comment |
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.
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 usestrdup
without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49
add a comment |
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.
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.
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 usestrdup
without a disclaimer that it's non-portable isn't a good idea.
– Lundin
Nov 13 '18 at 11:49
add a comment |
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 usestrdup
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
add a comment |
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', '>', '' ;
1
You should include the trailling NUL character when initializing with braces:..., '>', ''};
– Keine Lust
Nov 13 '18 at 7:47
add a comment |
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', '>', '' ;
1
You should include the trailling NUL character when initializing with braces:..., '>', ''};
– Keine Lust
Nov 13 '18 at 7:47
add a comment |
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', '>', '' ;
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', '>', '' ;
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
add a comment |
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
add a comment |
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 computingstrlen(name)
exactly once outside the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentiallyO(n**2)
by repeating thestrlen
teststrlen
times.– ShadowRanger
Nov 13 '18 at 4:05