我想问一下您对我如何在 main 函数上修复我的 set 变量的意见,即使在 realloc 正常工作之后它也只是出于某种原因保存第一个元素并在退出 get_set() 函数后丢弃其他元素。例如,输入:“2242 211 2334 112”将返回输出“2242 2242 2242 2242”。我坚持这一点,无法取得进展,我希望能得到比我在 C 方面更有经验的人的帮助,放心,在问这里之前,我已经尝试了我现在能想到的一切调试,改变代码等
c文件代码:
#define ENLARGE_SIZE(x) (x+1) * sizeof(int)
#define MAX_SIZE 214748364
size_t length = 0;
int* get_set_overloading(char* line);
int isDouble(const int* set, int find);
int* replace(int* set, const unsigned int at);
int* get_set()
{
char* output = calloc(1, sizeof(char)); /* Allocate memory for the output string and initialize it to an empty string*/
char* input = NULL; /* Buffer to store input line */
size_t input_size = 0; /* The initial size of the input buffer is 0*/
size_t output_size = 1; /* The initial size of the output string is 1*/
int c;
printf("Please enter below your set. ");
printf("Correct set should be with spaces after every number with only - before (without spaces betwen - and the number).");
printf("When you're ready to stop taking input, press ctrl+d .\n");
printf("Your set: ");
/* Read input line by line*/
while ((c = getchar()) != EOF) {
/* Resize input buffer*/
input_size += 1;
input = realloc(input, input_size);
/* Check if allocation was successful */
if (input == NULL) {
printf("Error: Out of memory\n");
return NULL;
}
/* Add the character to the input buffer */
input[input_size-1] = (char)c;
/* Resize output buffer*/
output_size += 1;
output = realloc(output, output_size);
/* Check if allocation was successful */
if (output == NULL) {
printf("Error: Out of memory\n");
exit(EXIT_FAILURE);
}
/* Null-terminate the input buffer */
if (c == '\n') {
input[input_size-1] = ' ';
/* Concatenate input to output*/
strcat(output, input);
/* Reset input buffer */
input_size = 0;
free(input);
input = NULL;
}
}
printf("\n");
printf("Your input: %s \n", output);
/* Free memory*/
if(input != NULL)
free(input);
return get_set_overloading(output);
}
/**
* Overloading, we want to split the code of get_set for readability
* This function takes in a pointer to a character array (string) as input,
* and returns a pointer to an integer array. The integer array contains
* the unique numbers parsed from the input string, ignoring duplicates.
*/
int* get_set_overloading(char* line)
{
int* array = NULL;
char* number = NULL;
bool hasSign = false;
int i;
int j = 0;
int at = 0;
number = calloc(MAX_SIZE, sizeof(char));
array = (int*)malloc(sizeof(int));
if(array == NULL)
{
printf("Failed to allocate memory to array\n");
if(number != NULL)
free(number);
return NULL;
}
if(number == NULL)
{
printf("Failed to allocate memory to number\n");
if(array != NULL)
free(array);
return NULL;
}
/* Parse the input string character by character */
while (*line != '\0')
{
if(!(*line == ' ' || *line == '-' || *line == '+' || (*line >= '0' && *line <= '9')) || ((*line == '-' || *line == '+') && hasSign))
{
printf("Error! Incorrect character has been typed. \n");
printf("Exiting the program.\n");
return NULL;
}
if(*line == ' ' && number != NULL)
{
if(length == 1)
{
array[length++] = atoi(number);
memset(number, 0, MAX_SIZE);
j=0;
}
else if(*(line-1) != ' ')
{
array = (int*)realloc(array, ENLARGE_SIZE(length));
if(array == NULL)
{
printf("Failed to allocate memory to array\n");
if(number != NULL)
free(number);
return NULL;
}
array[length++] = atoi(number);
memset(number, 0, MAX_SIZE);
j=0;
}
hasSign = false;
}
else if(*line == '-' || *line == '+')
hasSign = true;
/* Move to the next character in the input string and store the number */
number[j++] = *line;
line++;
}
if(*(line-1) != ' ')
{
array = (int*)realloc(array, ENLARGE_SIZE(length));
array[length++] = atoi(number);
}
free(number);
/* Remove duplicates from the array */
for(i = 0; i < length; i++)
{
if((at = isDouble(array, array[i])) != -1)
{
array = replace(array, at);
length--;
}
}
for(i = 0; i < length; i++)
{
printf("%d \n", array[i]);
}
printf("\n");
/* Return a pointer to the integer array */
return array;
}
int main(void)
{
int* set = get_set();
print_set(set);
free(set);
return 0;
}
如果能得到任何帮助,我将不胜感激。
编辑: 问题在这里:
int main(void)
{
int* set = get_set();
print_set(set);
free(set);
return 0;
}
它存储它从输入中获得的第一个元素,没有别的,对于“2242 211 2334 112”,它只会存储 2242
问题是空终止符和打印函数在一起