无论出于什么原因,编译器都会在我的字符数组中标记一个错误,指出缺少终止字符。
有趣的是,这个数组是我的老师提供的,目的是让我们学习指针,但他的数组/字符串有问题。
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"}; 发布于 2014-02-20 13:36:34
很难判断您的代码实际上是如何格式化的。
字符串文字不能跨越多个源码行,除非您使用反斜杠来转义行尾(但这不是最好的方法)。例如,如下所示:
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.";(我省略了大部分文本。)
发布于 2014-02-20 14:02:23
您可以在行尾使用\。这也是可行的。
像这样:
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"}; https://stackoverflow.com/questions/21898752
复制相似问题