'Problem assigning char values using pointers
I'm supposed to write a program which is able to store a string with an unknown length, which is received from the user's input. I need to use realloc so if the string's length is larger than the current array's length, it will re-allocate the needed memory to continue the process. It also needs to count the overall number of characters and number of alpha-numeric characters. It should also print the string in a nicely manner.
Now, my main problem is assigning the actual value to the pointer's address. When I print the current element, it prints garbage values and I just can't figure why. I declared a char* variable and used malloc to initiate an array, yet even at the point of assigning the input to the 'current' variable with getchar(), it seems like it doesn't work well.
Here's my code:
#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;
}
Solution 1:[1]
If you use the debugging and see the values changing, you will see the getchar didn't get value
To save it, I think you should write the assignment operation code in the loop body, but not after while
Below is the code I changed
#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;
}
Only changed some code near to the while function
And by the way, the break debugging of VS is really a useful tool
I am not sure if you use VS for coding, but if yes, you must know this useful tool, it really can solve most of the problems
Below is the link for breakpoint and debug in vs:
https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2022
Hope it can help you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | umifleur |
