C pointers: copy strings
up vote
1
down vote
favorite
I am trying to copy the string contained in
char *src
into
char *dst
this is my function but doesn't compile
char *mystrncpy(char *dst, char *src, size_t len)
{
char *temp = dst;
char *p = dst;
size_t i = 0;
for (p = dst; i < len; p += 1)
{
*p = *(src + p);
i += 1;
}
return *temp;
}
c string pointers
add a comment |
up vote
1
down vote
favorite
I am trying to copy the string contained in
char *src
into
char *dst
this is my function but doesn't compile
char *mystrncpy(char *dst, char *src, size_t len)
{
char *temp = dst;
char *p = dst;
size_t i = 0;
for (p = dst; i < len; p += 1)
{
*p = *(src + p);
i += 1;
}
return *temp;
}
c string pointers
*p = *(src + p);
shouldn't that be something among the lines of*p = *(src + i);
instead?
– Blaze
Nov 22 at 14:55
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
1
what is the purpose ofp < src
?
– Brice
Nov 22 at 15:03
1
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
2
The behavior ofp < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copylen
characters, so onlylen
is needed to control the loop.
– Eric Postpischil
Nov 22 at 15:08
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to copy the string contained in
char *src
into
char *dst
this is my function but doesn't compile
char *mystrncpy(char *dst, char *src, size_t len)
{
char *temp = dst;
char *p = dst;
size_t i = 0;
for (p = dst; i < len; p += 1)
{
*p = *(src + p);
i += 1;
}
return *temp;
}
c string pointers
I am trying to copy the string contained in
char *src
into
char *dst
this is my function but doesn't compile
char *mystrncpy(char *dst, char *src, size_t len)
{
char *temp = dst;
char *p = dst;
size_t i = 0;
for (p = dst; i < len; p += 1)
{
*p = *(src + p);
i += 1;
}
return *temp;
}
c string pointers
c string pointers
edited Nov 22 at 15:20
asked Nov 22 at 14:53
cucklordv4
113
113
*p = *(src + p);
shouldn't that be something among the lines of*p = *(src + i);
instead?
– Blaze
Nov 22 at 14:55
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
1
what is the purpose ofp < src
?
– Brice
Nov 22 at 15:03
1
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
2
The behavior ofp < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copylen
characters, so onlylen
is needed to control the loop.
– Eric Postpischil
Nov 22 at 15:08
add a comment |
*p = *(src + p);
shouldn't that be something among the lines of*p = *(src + i);
instead?
– Blaze
Nov 22 at 14:55
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
1
what is the purpose ofp < src
?
– Brice
Nov 22 at 15:03
1
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
2
The behavior ofp < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copylen
characters, so onlylen
is needed to control the loop.
– Eric Postpischil
Nov 22 at 15:08
*p = *(src + p);
shouldn't that be something among the lines of *p = *(src + i);
instead?– Blaze
Nov 22 at 14:55
*p = *(src + p);
shouldn't that be something among the lines of *p = *(src + i);
instead?– Blaze
Nov 22 at 14:55
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
1
1
what is the purpose of
p < src
?– Brice
Nov 22 at 15:03
what is the purpose of
p < src
?– Brice
Nov 22 at 15:03
1
1
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
2
2
The behavior of
p < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copy len
characters, so only len
is needed to control the loop.– Eric Postpischil
Nov 22 at 15:08
The behavior of
p < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copy len
characters, so only len
is needed to control the loop.– Eric Postpischil
Nov 22 at 15:08
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You're overdoing it.
You probably want this:
char *mystrncpy(char *dst, char *src, size_t len)
{
while (len > 0)
{
*dst++ = *src++;
len--;
}
return dst - len;
}
This is totally wrong
return *temp;
*temp
is a char
an not a pointer so return temp;
would be correct.
BTW: strictly speaking we are not copying a string as a string in C is a NUL terminated sequence of chars. Here we are just copying len
bytes.
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
@cucklordv4 not quite, consider what the++
is doing todst
.
– Jabberwocky
Nov 22 at 15:11
1
@Jabberwocky Pointer arithmetic withlen
will work as well.
– Govind Parmar
Nov 22 at 15:18
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
|
show 6 more comments
up vote
2
down vote
You are really close; your error is on this line:
*p = *(src + p);
Think about why you are dereferencing *(src + p)
... remember, where is the memory location of the character you're trying to copy?
Also your function should either return void
since there's no return
statement anywhere, or return dst
. Also, you don't need p < src
in your loop guard.
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to returnvoid
, the intent may have been that the function returns the destination, as the standardstrncpy
does.
– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You're overdoing it.
You probably want this:
char *mystrncpy(char *dst, char *src, size_t len)
{
while (len > 0)
{
*dst++ = *src++;
len--;
}
return dst - len;
}
This is totally wrong
return *temp;
*temp
is a char
an not a pointer so return temp;
would be correct.
BTW: strictly speaking we are not copying a string as a string in C is a NUL terminated sequence of chars. Here we are just copying len
bytes.
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
@cucklordv4 not quite, consider what the++
is doing todst
.
– Jabberwocky
Nov 22 at 15:11
1
@Jabberwocky Pointer arithmetic withlen
will work as well.
– Govind Parmar
Nov 22 at 15:18
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
|
show 6 more comments
up vote
3
down vote
accepted
You're overdoing it.
You probably want this:
char *mystrncpy(char *dst, char *src, size_t len)
{
while (len > 0)
{
*dst++ = *src++;
len--;
}
return dst - len;
}
This is totally wrong
return *temp;
*temp
is a char
an not a pointer so return temp;
would be correct.
BTW: strictly speaking we are not copying a string as a string in C is a NUL terminated sequence of chars. Here we are just copying len
bytes.
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
@cucklordv4 not quite, consider what the++
is doing todst
.
– Jabberwocky
Nov 22 at 15:11
1
@Jabberwocky Pointer arithmetic withlen
will work as well.
– Govind Parmar
Nov 22 at 15:18
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
|
show 6 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You're overdoing it.
You probably want this:
char *mystrncpy(char *dst, char *src, size_t len)
{
while (len > 0)
{
*dst++ = *src++;
len--;
}
return dst - len;
}
This is totally wrong
return *temp;
*temp
is a char
an not a pointer so return temp;
would be correct.
BTW: strictly speaking we are not copying a string as a string in C is a NUL terminated sequence of chars. Here we are just copying len
bytes.
You're overdoing it.
You probably want this:
char *mystrncpy(char *dst, char *src, size_t len)
{
while (len > 0)
{
*dst++ = *src++;
len--;
}
return dst - len;
}
This is totally wrong
return *temp;
*temp
is a char
an not a pointer so return temp;
would be correct.
BTW: strictly speaking we are not copying a string as a string in C is a NUL terminated sequence of chars. Here we are just copying len
bytes.
edited Nov 22 at 15:25
answered Nov 22 at 15:09
Jabberwocky
26.4k93769
26.4k93769
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
@cucklordv4 not quite, consider what the++
is doing todst
.
– Jabberwocky
Nov 22 at 15:11
1
@Jabberwocky Pointer arithmetic withlen
will work as well.
– Govind Parmar
Nov 22 at 15:18
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
|
show 6 more comments
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
@cucklordv4 not quite, consider what the++
is doing todst
.
– Jabberwocky
Nov 22 at 15:11
1
@Jabberwocky Pointer arithmetic withlen
will work as well.
– Govind Parmar
Nov 22 at 15:18
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
my function needs to return the dst string
– cucklordv4
Nov 22 at 15:10
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
I think i should do return dst; (is it correct?)
– cucklordv4
Nov 22 at 15:11
1
1
@cucklordv4 not quite, consider what the
++
is doing to dst
.– Jabberwocky
Nov 22 at 15:11
@cucklordv4 not quite, consider what the
++
is doing to dst
.– Jabberwocky
Nov 22 at 15:11
1
1
@Jabberwocky Pointer arithmetic with
len
will work as well.– Govind Parmar
Nov 22 at 15:18
@Jabberwocky Pointer arithmetic with
len
will work as well.– Govind Parmar
Nov 22 at 15:18
1
1
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
@GovindParmar sure, there are different possibilities.
– Jabberwocky
Nov 22 at 15:18
|
show 6 more comments
up vote
2
down vote
You are really close; your error is on this line:
*p = *(src + p);
Think about why you are dereferencing *(src + p)
... remember, where is the memory location of the character you're trying to copy?
Also your function should either return void
since there's no return
statement anywhere, or return dst
. Also, you don't need p < src
in your loop guard.
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to returnvoid
, the intent may have been that the function returns the destination, as the standardstrncpy
does.
– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
add a comment |
up vote
2
down vote
You are really close; your error is on this line:
*p = *(src + p);
Think about why you are dereferencing *(src + p)
... remember, where is the memory location of the character you're trying to copy?
Also your function should either return void
since there's no return
statement anywhere, or return dst
. Also, you don't need p < src
in your loop guard.
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to returnvoid
, the intent may have been that the function returns the destination, as the standardstrncpy
does.
– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
add a comment |
up vote
2
down vote
up vote
2
down vote
You are really close; your error is on this line:
*p = *(src + p);
Think about why you are dereferencing *(src + p)
... remember, where is the memory location of the character you're trying to copy?
Also your function should either return void
since there's no return
statement anywhere, or return dst
. Also, you don't need p < src
in your loop guard.
You are really close; your error is on this line:
*p = *(src + p);
Think about why you are dereferencing *(src + p)
... remember, where is the memory location of the character you're trying to copy?
Also your function should either return void
since there's no return
statement anywhere, or return dst
. Also, you don't need p < src
in your loop guard.
edited Nov 22 at 15:08
answered Nov 22 at 15:01
Govind Parmar
6,92553053
6,92553053
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to returnvoid
, the intent may have been that the function returns the destination, as the standardstrncpy
does.
– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
add a comment |
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to returnvoid
, the intent may have been that the function returns the destination, as the standardstrncpy
does.
– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
why I don't need p < src ?
– cucklordv4
Nov 22 at 15:06
1
1
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
@cucklordv4 other question: why do you think you need it?
– Jabberwocky
Nov 22 at 15:07
Rather than changing the type to return
void
, the intent may have been that the function returns the destination, as the standard strncpy
does.– Eric Postpischil
Nov 22 at 15:07
Rather than changing the type to return
void
, the intent may have been that the function returns the destination, as the standard strncpy
does.– Eric Postpischil
Nov 22 at 15:07
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
@EricPostpischil Updated my answer.
– Govind Parmar
Nov 22 at 15:08
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%2f53433538%2fc-pointers-copy-strings%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
*p = *(src + p);
shouldn't that be something among the lines of*p = *(src + i);
instead?– Blaze
Nov 22 at 14:55
yes it does compile but doesn't do what expected
– cucklordv4
Nov 22 at 14:58
1
what is the purpose of
p < src
?– Brice
Nov 22 at 15:03
1
If it does not compile you should include the error message in the question.
– Broman
Nov 22 at 15:03
2
The behavior of
p < src
is not defined by the C standard. You should not compare pointers from different objects for order. (Pointers may be compared for equality. Pointers within one object, such as an array or structure, may be compared for order.) Nor is there any reason to do such a comparison; the function is apparently designed to copylen
characters, so onlylen
is needed to control the loop.– Eric Postpischil
Nov 22 at 15:08