gpt4 book ai didi

c - int 被读作指针

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:16 28 4
gpt4 key购买 nike

<分区>

我必须从用户那里获取一个短语,并将其打印在中间有空格的倒三角形中。我的程序采用该短语,将其存储到一个 inputBuffer(char 数组)中,然后创建一个双倍于字符串大小的新数组。它用空格填充前半部分,用字符串本身填充后半部分。我想打印出新数组 strLength 次中的 strLength 字符,只需每次将 strLength 的范围向左移动 1 到 (strLength*2-1)。这确保在第一次迭代中,只打印整个字符串,第二次不打印开头的一个空格和结尾的一个字符,依此类推。
目前我收到一个错误,即使通过 strLength是一个 int 变量,当我用它来创建新数组时,它显然不是一个常量值。

int main(void) {

char inputBuffer[256];
char *pointer = inputBuffer;
char *temp = pointer;
int strLength = 0;

printf("enter your word: ");
scanf("%s", pointer);

//Calculate string length
while (*temp++) strLength++;

//Create an array double the size, first half for white spaces, and second half for the phrase.
char inputString[strLength * 2];
// ERROR: above expression inside the index must be a constant value.
int i, j;

//First half of array is number of spaces == number of char in phrase

for (i = 0; i < strLength; i++) {
inputString[i] = ' ';
}

//Reinitialize temp to use instead of pointer & put the string in the second half of inputString[]

temp = pointer;
for (j = 0; j < strLength; j++) {
inputString[i++] = *temp++;
}

//Just print the strLength indexes of inputStrng[] starting from half to end, and keep shifting the range by 1 position to the left.

for (i = strLength; i < (strLength * 2); i--) {
for (j = 0; j < strLength; j++) {
putchar(inputString[i + j]);
putchar(' ');
}
putchar('\n');
}

return 0;
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com