尝试这种情况:
char str[60 + 1]; /* Make target LARGE enough. */
char * p = str; /* Get at pointer to the target's 1st element. */
for(int n = 0; /* INITIALISE counter. */
n<60;
n++)
{
*p = datapoint*25+65; /* Store value by DE-referencing the pointer before
assigning the value to where it points. */
p++; /* Increment pointer to point to next element in target. */
}
*p = '\0'; /* Apply `0`-terminator using octal notation,
mind the angle of the slash! */
puts(str); /* Print the result to the console, note that it might (partly) be
unprintable, depending on the value of datapoint. */
而不指针当前元素替代的方法,但使用索引:
char str[60 + 1]; /* Make target LARGE enough. */
for(int n = 0; /* INITIALISE counter. */
n<60;
n++)
{
str[n] = datapoint*25+65; /* Store value to the n-th element. */
}
str[n] = '\0'; /* Apply `0`-terminator using octal notation,
mind the angle of the slash! */
来源
2016-04-26 18:09:47
alk
一个'char'类型的变量存储一个字符只要。在'char str'中,你不能存储''abcdefg'''甚至''ab'',但只能存储''a'',(注意不同的引号)。 – alk
也许你想要一个* char *的数组。 –