Thread: Entering string variable size

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003

    Entering string variable size

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define BUFSIZE 11
    int main()
    	char buffer[BUFSIZE];
    	char *string;
    	printf("Enter string: ");
    	string=(char *)malloc(sizeof(char)*strlen(buffer)+1);
    	if((strchr(buffer,'\n'))!=NULL) puts(string);
    	string=(char *)realloc(string,sizeof(char)*(strlen(string)+strlen(buffer))+1);
    	return 0;
    I'was trying to make simple program that allows user
    to enter string variable size with minimum wasting of memory.
    So I figure out this, I wrote thisjust to test fgets() function, so I define BUFSZIE as 11.
    Maybe this can be accomplished with less code, so if someone have some corection to suggest

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    This is a nonstandard function that you didn't include the header for. Even if you did include the header, clearing the screen is often unnecessary and anti-social. What if I had data from a previous program on my terminal when I ran your program? I would lose it, and hate you for causing me to lose it.

    >printf("Enter string: ");
    You didn't print a newline in your string, so the program isn't obliged to flush the stream. This prompt may not appear on some platforms, you should use fflush the stream:
    printf("Enter string: ");
    Always check return values from interactive input.

    >string=(char *)malloc(sizeof(char)*strlen(buffer)+1);
    The cast to char* is not required in C, and can hide the error if you forget to include stdlib.h. There's no need to multiply by the sizeof char because it will always be 1, and strlen returns a size_t type, so you will not get warnings about type mistmatches:
    string = malloc(strlen(buffer)+1);
    >string=(char *)realloc(string,sizeof(char) * (strlen(string)+strlen(buffer))+1);
    My comments for malloc apply here as well, but you should also consider that when realloc fails, it returns a null pointer. Thus, you would lose your only reference to the memory because NULL is then immediately assigned to string. A better way would be:
    char *save = realloc(string, strlen(string)+strlen(buffer)+1);
    if (save != NULL)
      string = save;
    else {
      /* Handle the error */
    Finally, your indention needs work. If you are using tabs, don't. Tabs are evil, replace them with spaces and your indention will be preserved everywhere.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Kick butt Prelude, that quote at the bottom of your post is the same one I have as my desktop from


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM