can a viriable be size of array when defining/creating array?

can a viriable be size of array when defining/creating array? which is not allowed in original C/C++

how about now?

eg:

{
int size;
size = 5*2;
char name; /creating array which size is variable/

name[0] = ‘a’;
name[1] = ‘b’;
name[2] = ‘c’;
name[3] = ’ ';
name[4] = ‘d’;
name[5] = ‘e’;
name[6] = ‘f’;
name[7] = ’ ';
name[3] = ‘\0’;
}

can a viriable be size of array when defining/creating array?

The compiler must know how much memory to allocate to store the array so the array size specifier must be an integer constant. It can be a const variable if that helps

What problem are you trying to solve ?

void someFn (int arraySize)
{
   char array [arraySize];
  ...
  ...
}

in your case, it actually looks like you just want to initialize a string

char name [] = "abc def ";

and you can use strlen() to determine the length of the string excluding the terminating NUL

int len = strlen (name);

in the more general case you can similar statically initialize arrays

int  data [] = { 1, 398, 9, 0, 88 };

and you can use sizeof() to determine the size of the array in bytes and the number of elements

#define N_DATA   (sizeof(data)/sizeof(int))

gcjr:

#define N_DATA   (sizeof(data)/sizeof(int))

Use an actual variable to avoid pre-processor macros and take advantage of the compiler's type checking and other diagnostics:

int  data [] = { 1, 398, 9, 0, 88 };
constexpr size_t numElements = (sizeof(data)/sizeof(data[0]));

should that be NumElements ?

ok, the main point is whether a dynamic variable is valid to indicate a size of arrary when array definition.
I learnt C in 1996, and the book and teacher told us again and again never use variable, const variable because the memory is allocated in compile for array, not in running, so need to a number.

eg:

  1. /wrong./
    const int size=3;
    int test;

  2. /wrong./
    int size;
    scanf("%d",&size);
    int test;

  3. /correct./
    #define size 3
    int test;

  4. /correct, same to 3 for compiler will extract Macro./
    int test[3];

and I see following piece is compiled ok,

#define buffer_length 128
uint8_t buffer[buffer_length]; /* this is ok,I see*/

uint8_t block_length = read_block(register, buffer, buffer_length); /* to read i2c, register is int type, buffer is a point, buffer_lenth is int type, return value is the number of value readed int type*/
uint8_t response_length = block_length + 1; /pay attention here 2 lines!!! to use viable to inicate size/
uint8_t response[response_length];/* to creating(define) an array which has one more element */
response[0] = register;/let the first element =register/

for (uint8_t i = 0; i < block_length; i++) /* use loop to fill last elements to new array*/
{
response[1 + i] = buffer*;*

  • }*
    for (uint8_t i = 0; i < response_length; i++) /* use loop to output new array*/
  • {*
    _ serial.print(response*);_
    _
    }_
    so this piece of code is to born a new array and insert an element before the first array than the old array.
    the most important is when these code are valide? is it a new C/C++ standard?
    uint8_t response_length = block_length + 1; /pay attention here 2 lines!!! to use viable to inicate size/
    uint8_t response[response_length];/ to creating(define) an array which has one more element */

    Thanks for everone.
    I did not use C for decades :frowning:

and now is it still a '\0' at the end of a string?
so we must define a one-more-element char array to hold it?
I am outofdate :frowning:

  1. /wrong./
    const int size=3;
    int test;[/quote]Your teacher was wrong.

youxiaojie:

  1. /wrong./ CORRECT
const int size=3;

int test[size];

and now is it still a ‘\0’ at the end of a string?
so we must define a one-more-element char array to hold it?

this

char name [] = "abc def ";

is the quivalent of

char name[size]; /*creating array which size is variable*/

name[0] = 'a';
name[1] = 'b';
name[2] = 'c';
name[3] = ' ';
name[4] = 'd';
name[5] = 'e';
name[6] = 'f';
name[7] = ' ';
name[8] = '0;
     // name[3] = '\0';                       <<<< why set the 3rd byte to NUL?
}

C handles strings intelligently. it follows the convention of terminating all strings with a NUL character. So the above string length (i.e. strlen()) is 8, but the size of the array (i.e. sizeof()) is 9