Home > database >  Why do i gett warnings by using strcpy_s and strcat_s?
Why do i gett warnings by using strcpy_s and strcat_s?

Time:01-15

I programmed an example on my Win64 PC with code:blocks using strcpy_s and strcat_s. The program works, but I get the warning messages "warning: implicit declaration of function 'strcpy_s' for strc_s and strcat_s respectively. The compiler settings have C11 enabled. And why can't I find the two functions in string.h?

// This program uses strcpy_s and strcat_s to build a phrase.

#include <string.h>     // for strcpy_s, strcat_s
#include <stdio.h>      // for printf

int main(void)
{
    char stringBuffer[80];

    strcpy_s(stringBuffer, sizeof(stringBuffer), "Hello world from ");
    strcat_s(stringBuffer, sizeof(stringBuffer), "strcpy_s ");
    strcat_s(stringBuffer, sizeof(stringBuffer), "and ");
    strcat_s(stringBuffer, sizeof(stringBuffer), "strcat_s!");

    printf("stringBuffer = %s\n", stringBuffer);
}

CodePudding user response:

From cppreference:

As with all bounds-checked functions, strcpy_s only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <string.h>.

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

See also: __STDC_LIB_EXT1__ availability in gcc and clang

CodePudding user response:

As near substitutes for the strcpy() and strcat() methods, the strcpy s() and strcat s() functions are described in ISO/IEC WDTR 24731. (). The maximum length of the destination buffer is specified by an additional rsize t type argument that is required by these procedures.

When there are no breaches of the constraints, the strcpy s() function is equivalent to strcpy(). The strcpy s() function duplicates characters from a source string—up to and including the terminating null character—to a destination character array. If the function is successful, it returns 0.

Only when the source string can be completely copied to the destination without filling the destination buffer does the strcpy s() function succeed. if the maximum size of the destination buffer is equal to zero, more than RSIZE MAX, or smaller, or if either the source or destination pointers are NULL

  • Related