嘗試這種情況:
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 *的數組。 –