0

I found this example at https://en.cppreference.com/w/c/string/byte/strcpy

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *src = "Take the test.";
//  src[0] = 'M' ; // this would be undefined behavior
    char dst[strlen(src) + 1]; // +1 to accomodate for the null terminator
    strcpy(dst, src);
    dst[0] = 'M'; // OK
    printf("src = %s\ndst = %s\n", src, dst);
 
#ifdef __STDC_LIB_EXT1__
    set_constraint_handler_s(ignore_handler_s);
    int r = strcpy_s(dst, sizeof dst, src);
    printf("dst = \"%s\", r = %d\n", dst, r);
    r = strcpy_s(dst, sizeof dst, "Take even more tests.");
    printf("dst = \"%s\", r = %d\n", dst, r);
#endif
}

Using nvcc compiler, I get an error on the line char dst[strlen(src) + 1]; "expression must have a constant value."

Using Visual C++, I get that error and another error, "C++ a value of type cannot be used to initialize an entity of type," on the line char *src = "Take the test.";

If I compile and run it on the site, it's fine.

Irelia
  • 3,407
  • 2
  • 10
  • 31
Rick the Scapegoat
  • 1,056
  • 9
  • 19
  • 5
    The example you took is C, not C++. – spectras May 07 '21 at 23:59
  • 6
    This is tagged C++ and it looks like you're trying to use C++ compilers, but you're linking to a code snippet from C documentation. C and C++ are not the same language and not all valid C is valid C++. – Nathan Pierson May 07 '21 at 23:59
  • 1
    In any case the two specific errors you're seeing are related to C and C++ differing on [whether variable-length arrays are permitted](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) and [whether you can assign a string literal to a char*](https://stackoverflow.com/questions/8356223/assign-a-string-literal-to-a-char). – Nathan Pierson May 08 '21 at 00:08
  • So I can't use nvcc? or Visual C++ compile a .c file? – Rick the Scapegoat May 08 '21 at 00:22
  • 1
    I don't know about nvcc but Visual C++ officialy doesn't support a lot of C features, for example these which you need here. – Luki446 May 08 '21 at 00:50
  • Please try to write your questions to have titles that would help someone with the same issue find and learn from the question and its answers. – Charles Duffy May 08 '21 at 02:55

1 Answers1

0

This feature is called "flexible array member" or in some cases "variable length arrays". It is supported in 'c' starting with 'c99' standard. gcc and microsoft 'c' supoorted it before the standard as well.

c++ in general does not support this feature. Some support was added in c++14 requireing simple expressions as array indexes.

So, you might be able to compile it with a 'c' compiler which does support c99 or later standards. Some specific qualifiers could be needed at the compilation command line as well.

The easiest way to work around it in a portable way would be using dynamic array allocation with malloc:

 char *dst = malloc(strlen(src) + 1);
 strcpy(dst, src);

and later use strlen(dst) instead of the sizeof dst;

Serge
  • 11,616
  • 3
  • 18
  • 28