Memory allocation for char array

后端 未结 5 1304
礼貌的吻别
礼貌的吻别 2021-02-03 14:46

I have a big problem with C language when it comes to strings, char * \'s or whatever... So in this particular case I have a huge problem. I want to create an array

5条回答
  •  清歌不尽
    2021-02-03 14:49

    First declare a pointer to a "char". Then ask (the system) for space to store your required number of values using malloc and then add elements to this "array".

    char * test;
    int num_of_elements = 99;
    test = malloc(sizeof(char) * num_of_elements); 
    //test points to first array elament
    test[0] = 11;
    test[1] = 22;
    //etc
    

提交回复
热议问题