C printf语句错误

C printf语句错误,c,C,我的上一个printf语句没有打印我不确定出了什么问题,因为我正在使用getchar读取标准输入,我还必须使用%s来解决这个问题。我试图让最后一个printf语句打印标准输入的精确输入 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define INITIAL_BUFFER_SIZE 16 // Input: pointer to the the old_Buffer i.e. *buf

我的上一个printf语句没有打印我不确定出了什么问题,因为我正在使用getchar读取标准输入,我还必须使用%s来解决这个问题。我试图让最后一个printf语句打印标准输入的精确输入

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define INITIAL_BUFFER_SIZE 16

// Input: pointer to the the old_Buffer i.e. *buffer and the pointer to the old_Buffer size i.e. *buffer_size
// Output: pointer to the new_Buffer i.e. *temp
// Summary: Function to increase the buffer size to double whenever the old_Buffer reaches the max possible size
char* IncreaseBuffer(char *buffer,int *buffer_size){

// Creating a new buffer of double the size
int temp_sz = *buffer_size;
temp_sz = 2*temp_sz;
char *temp;
temp = realloc(buffer, temp_sz);

// If the allocation is not successful, then exit the program and print the error
if (!temp) {
    printf("Error: Unable to Realloc\n");
    exit(EXIT_FAILURE);
}

// Update the buffer size as per the new buffer and return the new buffer instead of the old buffer
*buffer_size = temp_sz;
return temp;
}

int main(void){
// INITIAL BUFFER
char *myString;
myString = malloc(INITIAL_BUFFER_SIZE);
int curr_size = INITIAL_BUFFER_SIZE;

// TEMPORARY CHARACTER TO STORE THE READ CHARACTER
char ch;
printf("Enter a string: ");

// Number of buffer increases
int buff_inc = 0;

// Length of the string
int len = 0;

while((ch=getchar())!=EOF){
    if(ch == '\n'){
        // If character read is a new line, then we break and assume that we have got our string to print by now
        break;
    }else{
        if(len >= curr_size - 1){
            // If length of the string is greater than the buffer size, then we increase the buffer size
            // Also increment the number of buffer increases
            myString = IncreaseBuffer(myString, &curr_size);
            buff_inc++;
        }
        // Append the read character to the end of the buffer
        myString[len++] = ch;
    }
}

printf("String size: %d\n", len);
printf("Buffer increases: %d\n", buff_inc);
printf("You entered: %s\n",myString);

return 0;
}
#包括
#包括
#包括
#定义初始缓冲区大小16
//输入:指向旧_缓冲区的指针,即.*缓冲区,以及指向旧_缓冲区大小的指针,即.*缓冲区大小
//输出:指向新缓冲区的指针,即*temp
//小结:当旧缓冲区达到最大可能大小时,将缓冲区大小增加一倍的函数
字符*递增缓冲区(字符*缓冲区,整数*缓冲区大小){
//创建两倍大小的新缓冲区
int temp_sz=*缓冲区大小;
temp_sz=2*temp_sz;
字符*温度;
temp=realloc(缓冲区,temp_sz);
//如果分配不成功,则退出程序并打印错误
如果(!temp){
printf(“错误:无法重新定位\n”);
退出(退出失败);
}
//根据新缓冲区更新缓冲区大小,并返回新缓冲区而不是旧缓冲区
*缓冲区大小=温度;
返回温度;
}
内部主(空){
//初始缓冲区
char*myString;
myString=malloc(初始缓冲区大小);
int curr\u size=初始缓冲区大小;
//用于存储读取字符的临时字符
char ch;
printf(“输入字符串:”);
//缓冲区数量增加
int buff_inc=0;
//弦长
int len=0;
而((ch=getchar())!=EOF){
如果(ch='\n'){
//若字符读取是一个新行,那个么我们中断并假设现在已经得到了要打印的字符串
打破
}否则{
如果(长度>=当前大小-1){
//如果字符串的长度大于缓冲区大小,则增加缓冲区大小
//同时,缓冲区的数量也会增加
myString=IncreaseBuffer(myString和curr_size);
buff_inc++;
}
//将读取字符追加到缓冲区的末尾
myString[len++]=ch;
}
}
printf(“字符串大小:%d\n”,len);
printf(“缓冲区增加:%d\n”,buff_公司);
printf(“您输入了:%s\n”,myString);
返回0;
}
调用
realloc
后,您不能释放旧的缓冲区。这很糟糕。它已经调整了大小,内存分配也得到了处理。它甚至可能在重新分配后保留相同的地址

只需删除以下行:

// Free the old buffer
free(buffer);
buffer = NULL;
并且,在注释中已经指出,您忘记了终止字符串。循环结束后执行此操作:

myString[len] = '\0';

在刚刚看到@paddy的答案后,这篇文章被删减了,但作为记录,这篇文章来自文档:

成功后返回指向新分配内存开头的指针。返回的指针必须用free()或realloc()解除分配原始指针ptr无效,对它的任何访问都是未定义的行为(即使重新分配到位)

失败时,返回空指针。原始指针ptr仍然有效,可能需要用free()或realloc()解除分配


您需要NULL('\0')终止字符串。感谢您的帮助。但是,我仍然没有打印您输入的最后一条printf语句:@UmamaheshP:
NULL
是一个NULL指针常量<代码>'\0'是空字符。嗨,你们能帮我完成最后一个printf语句吗?我真的很困惑为什么不打印?