无论出于何种原因,编译器在我的字符数组上标记错误,说明缺少终止字符。
有趣的是,这个数组是由我的老师提供的,让我们了解指针,但他的数组/字符串有问题。
char Ar[] = {"All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae. Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind; and they are the nearest to the Germans, who dwell beyond the Rhine , with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either repel them from their own territories, or themselves wage war on their frontiers. One part of these, which it has been said that the Gauls occupy, takes its beginning at the river Rhone ; it is bounded by the river Garonne, the ocean, and the territories of the Belgae; it borders, too, on the side of the Sequani and the Helvetii, upon the river Rhine , and stretches toward the north.
From 'Caesar's Conquest of Gaul', Translator. W. A. McDevitte. Translator. W. S. Bohn. 1st Edition. New York. Harper & Brothers. 1869. Harper's New Classical Library. Published under creative commons and available at http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.02.0001"};
答案 0 :(得分:2)
很难说出你的代码是如何实际格式化的。
字符串文字不能跨越多个源行,除非您使用反斜杠来转义行结尾(但这不是最好的方法)。例如,这个:
char Ar[] = {"All Gaul is divided into three parts, one of which
the Belgae inhabit, the Aquitani another, those who in their own
language are called Celts, in our Gauls, the third."};
是语法错误。如果您希望将它作为一个没有换行符的长字符串,则可以使用连接 - 而不是strcat()
函数,而是使用语法功能来合并相邻的字符串文字:
char Ar[] = {"All Gaul is divided into three parts, one of which "
"the Belgae inhabit, the Aquitani another, those who in their "
"own language are called Celts, in our Gauls, the third."};
顺便提一下,大括号是允许的,但不是必需的:
char Ar[] = "All Gaul is divided into three parts, one of which "
"the Belgae inhabit, the Aquitani another, those who in their "
"own language are called Celts, in our Gauls, the third.";
如果您想要嵌入换行符,可以添加它们:
char Ar[] = "All Gaul is divided into three parts, one of which\n"
"the Belgae inhabit, the Aquitani another, those who in their\n"
"own language are called Celts, in our Gauls, the third.";
(我省略了大部分文字。)
答案 1 :(得分:2)
您可以在该行的末尾使用\
。那也行。
像:
char Ar[] = {"All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae. Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind; and they are the nearest to the Germans, who dwell beyond the Rhine , with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either repel them from their own territories, or themselves wage war on their frontiers. One part of these, which it has been said that the Gauls occupy, takes its beginning at the river Rhone ; it is bounded by the river Garonne, the ocean, and the territories of the Belgae; it borders, too, on the side of the Sequani and the Helvetii, upon the river Rhine , and stretches toward the north.\
From 'Caesar's Conquest of Gaul', Translator. W. A. McDevitte. Translator. W. S. Bohn. 1st Edition. New York. Harper & Brothers. 1869. Harper's New Classical Library. Published under creative commons and available at http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.02.0001"};