hey guys i have looked around for a solution and tried everything i can think of im new to pointers and dynamic strings and i could really do with some help with problem. im currently learning c and i need to get the user to input a dynamic size for the string length . i need to make it so the users input can not be bigger then 100 . here's where i am at currently . i have the code booting but if i try set the size to let's say 5 i can still input way more chars into the string. cheers really appreciate any help .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int maxSize = 100;
char *name = (char*) malloc (maxSize * sizeof (char));
int n;
char text1[]= ("input string size\n");
printf ("your string lenght is %d\n", strlen(name));
//getting size
n=intPrintScanner(text1);
printf ("your size is %d\n",n);
name = realloc (name, sizeof (char) * n);
//printing results
printf ("your string lenght is %d\n",strlen (name));
scanf("%s", name);
printf("your string is %s",name);
free(name);
fflush(stdin);
printf("press any key to close");
getchar();
return (0);
}
CodePudding user response:
Bugs:
You never assign any data to
nameso it just contains garbage. You can't usestrlenon it before you have stored valid data there either. You can store a string insidenameby for example usingstrcpy.When using
realloc, there's no guarantee that the old pointer is the same as the returned pointer. Also, you need error handling. Implement it like this instead:char* tmp = realloc (name, n); if(tmp == NULL) { /* actual error handling here in case realloc fails */ } } name = tmp; // assign pointer to the new area only if allocation succeededfflush(stdin);is not well-defined, never usefflushon input streams. Instead you should discard unwanted line feed characters fromstdin- which could be as trivial as an extragetchar()call just after reading something. Check out How to read / parse input in C? The FAQ for lots of general good advise regarding how to take input fromstdin.
Cosmetic/style:
No need for parenthesis here:
char text1[]= ("input string size\n");. All it achieves it to make the code look strange.The correct form of main is
int main (void). Theint main()is obsolete style.There is no need to wrap the expression passed to
returnin a parenthesis.There is never a need to multiply something with
sizeof (char), sincesizeof (char)is by definition always1no matter system.There is no need to cast the result of
malloc.
CodePudding user response:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
char* read_until(int fd, char end) {
int i = 0, size;
char c = '\0';
char* string = (char*)malloc(sizeof(char));
while (1) {
size = read(fd, &c, sizeof(char));
if (c != end && size > 0) {
string = (char*)realloc(string, sizeof(char) * (i 2));
string[i ] = c;
} else {
break;
}
}
string[i] = '\0';
return string;
}
int main()
{
char *name;
int correct=0;
do{
write(1,"Put a name: ",strlen("Put a name: "));
name = read_until(STDIN_FILENO,'\n');
if(strlen(name) > 99){
write(1,"Error\n",strlen("Error\n"));
}else{
correct=1;
}
}while(correct != 1);
write(1,name,strlen(name));
free(name);
}
Try using write and read instead of printf and scanf, it is better for allocating dynamic memory, read and try to understand the read_until function, there are better ways to do main.
