简体   繁体   中英

Array initialization needs curly braces

I have been trying to get this code to work to encrypt the *char[] pointer array with ROT13 encryption. Couple of problems:

  1. The program does not compile. The Error is: 'text': array initialization needs curly braces.
  2. ROT13 does not seem to be working properly. It saves the numeric value of the ASCII code rather than its equivalent letter.

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; , text is declared as an array of pointers to 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 **text = &text1;   

You should note that the declaration char * text[] in main and in the function parameter is not same. When declared as a function parameter char * text[] is equivalent to char **text .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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