Whats the proper way to handle strcpy's return type












0















I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL

int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here

if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}

int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}

char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}


Here is use a demo function which returns NULL to explain my problem.
I was thinking to check src before past it to strcpy but I really cannot understand why strcpy returns dest without check if src is NULL or not.



Why does this happens because I see that strcpy manual say only about the return of dest and nothing if fails:



   RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.









share|improve this question

























  • but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

    – Swordfish
    Nov 24 '18 at 17:34











  • strcpy causes the abort of your program because one of the parameters is null. Check before calling.

    – Paul Ogilvie
    Nov 24 '18 at 17:34











  • @Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

    – user10533142
    Nov 24 '18 at 17:36


















0















I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL

int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here

if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}

int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}

char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}


Here is use a demo function which returns NULL to explain my problem.
I was thinking to check src before past it to strcpy but I really cannot understand why strcpy returns dest without check if src is NULL or not.



Why does this happens because I see that strcpy manual say only about the return of dest and nothing if fails:



   RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.









share|improve this question

























  • but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

    – Swordfish
    Nov 24 '18 at 17:34











  • strcpy causes the abort of your program because one of the parameters is null. Check before calling.

    – Paul Ogilvie
    Nov 24 '18 at 17:34











  • @Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

    – user10533142
    Nov 24 '18 at 17:36
















0












0








0








I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL

int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here

if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}

int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}

char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}


Here is use a demo function which returns NULL to explain my problem.
I was thinking to check src before past it to strcpy but I really cannot understand why strcpy returns dest without check if src is NULL or not.



Why does this happens because I see that strcpy manual say only about the return of dest and nothing if fails:



   RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.









share|improve this question
















I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL

int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here

if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}

int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}

char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}


Here is use a demo function which returns NULL to explain my problem.
I was thinking to check src before past it to strcpy but I really cannot understand why strcpy returns dest without check if src is NULL or not.



Why does this happens because I see that strcpy manual say only about the return of dest and nothing if fails:



   RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.






c strcpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:50

























asked Nov 24 '18 at 17:29







user10533142




















  • but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

    – Swordfish
    Nov 24 '18 at 17:34











  • strcpy causes the abort of your program because one of the parameters is null. Check before calling.

    – Paul Ogilvie
    Nov 24 '18 at 17:34











  • @Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

    – user10533142
    Nov 24 '18 at 17:36





















  • but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

    – Swordfish
    Nov 24 '18 at 17:34











  • strcpy causes the abort of your program because one of the parameters is null. Check before calling.

    – Paul Ogilvie
    Nov 24 '18 at 17:34











  • @Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

    – user10533142
    Nov 24 '18 at 17:36



















but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

– Swordfish
Nov 24 '18 at 17:34





but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a NULL. And I bet strcpy() most times won't return anything but crash almost instantly.

– Swordfish
Nov 24 '18 at 17:34













strcpy causes the abort of your program because one of the parameters is null. Check before calling.

– Paul Ogilvie
Nov 24 '18 at 17:34





strcpy causes the abort of your program because one of the parameters is null. Check before calling.

– Paul Ogilvie
Nov 24 '18 at 17:34













@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

– user10533142
Nov 24 '18 at 17:36







@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc programmer I need to check everything against NULL but this is not my Question.

– user10533142
Nov 24 '18 at 17:36














1 Answer
1






active

oldest

votes


















1














You handle the situation by first checking the parameters are not null. For example:



int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}


Note also that dest cannot be const because it is written to.






share|improve this answer
























  • Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

    – user10533142
    Nov 24 '18 at 17:40













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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460686%2fwhats-the-proper-way-to-handle-strcpys-return-type%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









1














You handle the situation by first checking the parameters are not null. For example:



int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}


Note also that dest cannot be const because it is written to.






share|improve this answer
























  • Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

    – user10533142
    Nov 24 '18 at 17:40


















1














You handle the situation by first checking the parameters are not null. For example:



int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}


Note also that dest cannot be const because it is written to.






share|improve this answer
























  • Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

    – user10533142
    Nov 24 '18 at 17:40
















1












1








1







You handle the situation by first checking the parameters are not null. For example:



int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}


Note also that dest cannot be const because it is written to.






share|improve this answer













You handle the situation by first checking the parameters are not null. For example:



int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}


Note also that dest cannot be const because it is written to.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 17:39









Paul OgilviePaul Ogilvie

18.3k21235




18.3k21235













  • Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

    – user10533142
    Nov 24 '18 at 17:40





















  • Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

    – user10533142
    Nov 24 '18 at 17:40



















Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

– user10533142
Nov 24 '18 at 17:40







Well, that means that strcpy works after I know 100% that dest and src are OK. Thank you

– user10533142
Nov 24 '18 at 17:40






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460686%2fwhats-the-proper-way-to-handle-strcpys-return-type%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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen