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;
}









share|improve this question
























  • *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 copy len characters, so only len is needed to control the loop.
    – Eric Postpischil
    Nov 22 at 15:08















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;
}









share|improve this question
























  • *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 copy len characters, so only len is needed to control the loop.
    – Eric Postpischil
    Nov 22 at 15:08













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;
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 copy len characters, so only len 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










  • 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 copy len characters, so only len 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












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.






share|improve this answer























  • 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 to dst.
    – Jabberwocky
    Nov 22 at 15:11






  • 1




    @Jabberwocky Pointer arithmetic with len will work as well.
    – Govind Parmar
    Nov 22 at 15:18






  • 1




    @GovindParmar sure, there are different possibilities.
    – Jabberwocky
    Nov 22 at 15:18


















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.






share|improve this answer























  • 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 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











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',
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
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • 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 to dst.
    – Jabberwocky
    Nov 22 at 15:11






  • 1




    @Jabberwocky Pointer arithmetic with len will work as well.
    – Govind Parmar
    Nov 22 at 15:18






  • 1




    @GovindParmar sure, there are different possibilities.
    – Jabberwocky
    Nov 22 at 15:18















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.






share|improve this answer























  • 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 to dst.
    – Jabberwocky
    Nov 22 at 15:11






  • 1




    @Jabberwocky Pointer arithmetic with len will work as well.
    – Govind Parmar
    Nov 22 at 15:18






  • 1




    @GovindParmar sure, there are different possibilities.
    – Jabberwocky
    Nov 22 at 15:18













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 to dst.
    – Jabberwocky
    Nov 22 at 15:11






  • 1




    @Jabberwocky Pointer arithmetic with len 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










  • 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 to dst.
    – Jabberwocky
    Nov 22 at 15:11






  • 1




    @Jabberwocky Pointer arithmetic with len 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












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.






share|improve this answer























  • 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 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















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.






share|improve this answer























  • 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 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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


















  • 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 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo