[英]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: 几个问题:
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.