简体   繁体   English

数组初始化需要花括号

[英]Array initialization needs curly braces

I have been trying to get this code to work to encrypt the *char[] pointer array with ROT13 encryption. 我一直在尝试使此代码能够使用ROT13加密来加密*char[]指针数组。 Couple of problems: 几个问题:

  1. The program does not compile. 该程序无法编译。 The Error is: 'text': array initialization needs curly braces. 错误是:'text':数组初始化需要花括号。
  2. ROT13 does not seem to be working properly. ROT13似乎无法正常工作。 It saves the numeric value of the ASCII code rather than its equivalent letter. 它保存ASCII码的数字值,而不是其等效字母。

Here is my code: 这是我的代码:

void rot13(int numlines, char * text[]){
    //printf("%s\n", text);
    //char encrypted[length(text)];

    for (int i=0; text[i]>='\0'; i++){
        if (*text[i]>='A' && *text[i]<='Z'){
            *text[i]=(((*text[i]-'A')+13)%26 + 'A');
        }else if(*text[i]>='a' && *text[i]<='z'){
            *text[i]=(((*text[i]-'a')+13)%26 + 'a');
        }
    }

    printf ("%d\n ",*text);
}

int main(){
    char text1[]="parliament";
    char * text[]=&text1;
    rot13(10, text);
}

In char * text[]=&text1; char * text[]=&text1; , text is declared as an array of pointers to char . text被声明为指向char的指针的数组。 Therefore is is of an array type. 因此是数组类型。 It can't be initialized without using curly braces (exception: string literals). 如果不使用花括号,则无法对其进行初始化(例外:字符串文字)。 Better to declare it as pointer to pointer to char 最好将其声明为指向char的指针

char **text = &text1;   

You should note that the declaration char * text[] in main and in the function parameter is not same. 您应注意, main和function参数中的声明char * text[]不相同。 When declared as a function parameter char * text[] is equivalent to char **text . 当声明为函数参数时, char * text[]等同于char **text

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

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