【发布时间】:2021-06-30 06:44:54
【问题描述】:
我遇到了一个问题,这里的程序是一个数据输入例程,有问题的问题是变量 c 去 capac 函数询问用户字符串的最大容量,当它到达 for 的第 3 个周期时,它开始增长,天知道如何使我的 for 变得无法使用。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int func_carac(int *car) //function that catches characters introduced by the user
{
int caracter;
printf("\nType the character you want: \n");
caracter = getche();
*car = caracter;
return *car;
}
//________________________________________________________\\
int func_capac(int *cap) //function to ask the user for the amount of characters
{
int quant;
printf("\nDetermine the maximum character capacity you want to write: ");
scanf("%d",&quant);
*cap = quant;
return *cap;
}
int main()
{
int car, pos, c, total;
char vetor[] = {'\0'};
func_capac(&c);
total = c + 1;
vetor[total];
vetor[total] = '\0';
printf("%i", c);
for(pos = 0; pos < c; pos++)
{
func_carac(&car);
if((car >= 97) && (car <= 122) || (car == 32))
{
printf("\n%c - Tabela ASCII, %d \n",car ,car);
printf("%i", c);
vetor[pos] = (char) car;
}
else if(car == 8)
{
printf("Successfully removed the previous character.");
pos = pos-2;
}
else
{
pos--;
}
}
printf("Frase: %s\n",vetor);
}
如果有人可以帮助我,我将不胜感激
【问题讨论】:
-
在你的函数中,为什么你同时使用传递引用仿真和返回值?做一个或另一个,而不是两者都做(我建议只返回值)。
-
我认为你也标记了这个窗口,conio.h/getche 不是 posix。
-
至于你的问题,C 没有动态数组。数组
vector被定义为只包含一个元素。任何非零索引都将超出范围并导致未定义行为。 -
vetor[total];什么都不做。 -
@AllanWind 除了超出数组范围。
标签: c string function for-loop