简体   繁体   English

将字符串数组分配给char **

[英]Assigning array of strings to char **

In C, I know you can assign strings to char pointers, so by extension, why does this fail? 在C中,我知道你可以为char指针分配字符串,所以通过扩展,为什么会失败呢? So, I have a char double pointer, and say initially I want it to have certain values. 所以,我有一个char双指针,并说最初我希望它有一定的值。 Then I'm done using those and I want to re-assign it. 然后我完成了使用它们,我想重新分配它。

char **notes = {
    "C4", "C5", "A3", "A4", "A3#", "A4#", "REST",
    "C4", "C5", "A3", "A4", "A3#", "A4#", "REST",
    "F3", "F4", "D3", "D4", "D3#", "D4#", "REST",
    "F3", "F4", "D3", "D4", "D3#", "D4#", "REST",
    "D4#", "D4", "C4#", "C4", "D4#", "D4", "G3#",
    "G3", "C4#", "C4", "F4#", "F4", "E4", "A4#",
    "A4", "G4#", "D4#", "B3", "A3#", "A3", "G3#"
};

// do something with notes, then

notes = {
    "C3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "C3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "D3#", "D3", "C3#", "C3", "D3#", "D3", "G2#",
    "G2", "C3#", "C3", "F3#", "F3", "E3", "A3#",
    "A3", "G3#", "D3#", "B2", "A2#", "A2", "G2#"
};

I get the error: 我收到错误:

error: expected expression
notes = {
        ^

(Bonus points to anyone who can identify the song. Hint: switch the sharps to equivalent flats) (奖励指向任何可以识别歌曲的人。提示:将锐利切换到等效的平面)

You should initialize it where it is declared and not as a separate step. 您应该在声明它的位置初始化它,而不是作为单独的步骤。

char** notes = {
    "C3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "C3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "D3#", "D3", "C3#", "C3", "D3#", "D3", "G2#",
    "G2", "C3#", "C3", "F3#", "F3", "E3", "A3#",
    "A3", "G3#", "D3#", "B2", "A2#", "A2", "G2#"
};

The above causes undefined behavior if you try to modify it later on. 如果您稍后尝试修改它,上面会导致未定义的行为。 If you want a mutable array, one option is using the 2 dimensional array notation ( char notes[][4] = ... ), but that would lead to waste of space in this format, because you would need 4 characters to be stored per element as the longest character array is 4 in length, 3 non-nulls and one null. 如果你想要一个可变数组,一个选项是使用二维数组表示法( char notes[][4] = ... ),但这会导致这种格式的空间浪费,因为你需要4个字符每个元素存储,因为最长的字符数组长度为4,非空值为3,空值为1。

Update: If you're using C99, there are compound literals that allow you to make the right hand side an expression and hence reassign to it in the following manner. 更新:如果您正在使用C99,则可以使用复合文字来使右侧成为表达式,从而以下列方式重新分配给它。

char** notes;
notes = (char*[]) {
    "D3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "C3", "C4", "A2", "A3", "A2#", "A3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "F2", "F3", "D2", "D3", "D2#", "D3#", "REST",
    "D3#", "D3", "C3#", "C3", "D3#", "D3", "G2#",
    "G2", "C3#", "C3", "F3#", "F3", "E3", "A3#",
    "A3", "G3#", "D3#", "B2", "A2#", "A2", "G2#"
};

Example: https://ideone.com/eD1xFu 示例: https//ideone.com/eD1xFu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM