Thread: strcpy results null

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    strcpy results null

    Code:
    int main() {    char x[] = "Happy Birthday to You";
        char y[25];
        char z[15];
    
    
        printf( "%s%s\n%s%s\n",
     "The string in array x is: ", x,
     "The string in array y is: ",strcpy_s( y, x ) );
        strncpy_s(z,x,14);
        z[14]='\0';
        printf("\nz is %s",z);
    
    
        _getch(); 
        return 0; 
    }
    why is y printed as <null>?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It works for me. Admittedly I used strcpy rather than strcpy_s, but that should make no difference in this case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    In an online compiler the code worked.

    I included <string.h> library, maybe I should add anything else?

    P.S. strcat, strncat don't work either (print null as a result)
    Last edited by lmanukyan; 01-29-2016 at 03:28 AM.

  4. #4
    Registered User naaissus's Avatar
    Join Date
    Jan 2016
    Location
    Balkan
    Posts
    23
    I guess that this is the problem: strcopy_s requires three parameters:
    https://msdn.microsoft.com/en-us/library/td1esda9.aspx

    strDestination Location of the destination string buffer.
    numberOfElements Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t units for wide functions.
    strSource Null-terminated source string buffer.
    So what if you try to put:
    Code:
    strcpy_s(y, sizeof(y), x)
    or something like that?

    https://msdn.microsoft.com/en-us/library/d45bbxx4.aspx
    https://msdn.microsoft.com/en-us/library/w6w3kbaf.aspx

    I guess that you are using Visual Studio? If you try to use functions without "_s" it generates warning?
    If you want to use functions without an "_s" and with standard number of arguments, you need to add #define _CRT_SECURE_NO_WARNINGS at the beginning of the document.
    Last edited by naaissus; 01-29-2016 at 03:38 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lmanukyan
    I included <string.h> library, maybe I should add anything else?
    I assumed that you left out the header inclusions as they were obvious. Remind me to stop assuming that for you.

    This is what gcc 4.8.4 complains about for your code in post #1 when compiling with -Wall -pedantic -std=c99 (or -ansi):
    Code:
    test.c: In function ‘main’:
    test.c:6:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
         printf( "%s%s\n%s%s\n",
         ^
    test.c:6:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    test.c:8:2: warning: implicit declaration of function ‘strcpy_s’ [-Wimplicit-function-declaration]
      "The string in array y is: ",strcpy_s( y, x ) );
      ^
    test.c:8:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat=]
    test.c:9:5: warning: implicit declaration of function ‘strncpy_s’ [-Wimplicit-function-declaration]
         strncpy_s(z,x,14);
         ^
    test.c:14:5: warning: implicit declaration of function ‘_getch’ [-Wimplicit-function-declaration]
         _getch(); 
         ^
    Actually, it also gives me these link errors, but these you can ignore them if they are supported:
    Code:
    In function `main':
    test.c:(.text+0x55): undefined reference to `strcpy_s'
    test.c:(.text+0x97): undefined reference to `strncpy_s'
    test.c:(.text+0xbb): undefined reference to `_getch'
    You should include the relevant headers to silence these warnings, and then pay attention to any further warnings that are emitted. If there are no warnings to begin with, you must compile at a higher warning level. If you have been ignoring warnings, then start paying attention to them, or all you will get from me are the warnings from my compiler, if I bother to post at all when encountering warnings from your code.
    Last edited by laserlight; 01-29-2016 at 03:33 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Maybe you should reread your documentation on strcpy_s. Especialy the amount of parameters it uses and its return value.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  2. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  3. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  4. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  5. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM