[英]Problem assigning char values using pointers
我应该编写一个程序,该程序能够存储从用户输入中接收到的长度未知的字符串。 我需要使用realloc
,所以如果字符串的长度大于当前数组的长度,它将重新分配所需的 memory 以继续该过程。 它还需要计算字符总数和字母数字字符数。 它还应该以一种很好的方式打印字符串。
现在,我的主要问题是将实际值分配给指针的地址。 当我打印当前元素时,它会打印垃圾值,我只是想不通为什么。 我声明了一个char*
变量并使用malloc
来启动一个数组,但即使在使用getchar()
将输入分配给“当前”变量时,它似乎也不能正常工作。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define BUFFER 10
#define MAX_CHAR 50
void uniPrint(char, int);
int main() {
int alphaCount = 0;
int totalCount = 0;
int i = 0;
int j = 0;
char current;
char* temp;
char *ptr = (char*)malloc(BUFFER * sizeof(char));
if(!ptr)
{
printf("\nError: Memory could not be allocated. \n");
exit(0);
}
printf("Please enter some text and press CTRL+D when you are done: \n");
while((current = getchar() != EOF))
{
if(i % BUFFER == 0) {
temp = (char*)realloc(ptr, sizeof(char)*(i) + BUFFER);
if(!temp)
{
printf("\nMemory could not be allocated. \n");
exit(0);
}
ptr = temp;
}
ptr[i] = current;
putchar(current);
if(isalnum(current))
alphaCount++;
totalCount++;
i++;
}
for(j = 0; j < i; j++)
{
uniPrint(ptr[j], j);
}
free(ptr);
printf("\nTotal number of alpha-numeric characters is: %d\n", alphaCount);
printf("Total number of characters is: %d\n", totalCount);
return 0;
}
void uniPrint(char c, int j)
{
if(!(j % MAX_CHAR))
printf("\n");
putchar(c);
return;
}
如果您使用调试并看到值发生变化,您将看到 getchar 没有获得值
为了省事,我觉得应该把赋值操作的代码写在循环体里,而不是在while之后
下面是我更改的代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define BUFFER 10
#define MAX_CHAR 50
void uniPrint(char, int);
int main() {
int alphaCount = 0;
int totalCount = 0;
int i = 0;
int j = 0;
char current;
char* temp;
char *ptr = (char*)malloc(BUFFER * sizeof(char));
if (!ptr)
{
printf("\nError: Memory could not be allocated. \n");
exit(0);
}
printf("Please enter some text and press ENTER when you are done: \n");//I am not understand why not use enter
while (1)//Here, should write expression for looping judgement only, but not assignment
{
current = getchar();//Write the assignment in the loop body
if (current != '\n')//Condition for loop
{
if (i % BUFFER == 0 && i != 0) {//I think it shouldn't use realloc at the beginning
temp = (char*)realloc(ptr, sizeof(char)*(i)+BUFFER);
if (!temp)
{
printf("\nMemory could not be allocated. \n");
exit(0);
}
ptr = temp;
}
ptr[i] = current;
putchar(current);
if (isalnum(current))
alphaCount++;
totalCount++;
i++;
}
else
break;
}
for (j = 0; j < i; j++)
{
uniPrint(ptr[j], j);
}
free(ptr);
printf("\nTotal number of alpha-numeric characters is: %d\n", alphaCount);
printf("Total number of characters is: %d\n", totalCount);
return 0;
}
void uniPrint(char c, int j)
{
if (!(j % MAX_CHAR))
printf("\n");
putchar(c);
return;
}
只在 while function 附近更改了一些代码
顺便说一句,VS的break debugging真是个好用的工具
我不确定你是否使用VS进行编码,但如果是,你必须知道这个好用的工具,它确实可以解决大部分问题
下面是vs中断点调试的链接:
https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2022
希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.