Thread: Problems with dynamic memory and strings

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Problems with dynamic memory and strings

    Hi everyone! I've started learning C a couple of months ago and now I'm facing a couple of problems when it comes to dynamic memory.
    You see, I was trying to make a program that could read as many characters as the user would type. Everything was working fine but I couldn't print out the whole string. Any suggestions would be very much appreciated

    Thanks in advance!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    
    int main ()
    {
     char *c = (char*)malloc(sizeof(char));
     int x = 0;
     do
     {
         c = (char*)realloc(c, sizeof(char)*(x+1));
         *(c+x) = getchar();
         x++;
     }while (*(c+x) != '\0');
    
    
    for (int y = 0; y < x; y++ )
     {
       printf("%c", c[y]);
     }
     getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    A few things.

    *(c+x) can be written c[x], which tends to be easier to read.

    getchar() does not return '\0' to mean “end of input”. Rather, it returns EOF; if you want to stop when the user hits enter, then you need to check for '\n'.

    However, due to the way getchar() works, if you want to check for EOF, you have to assign to an int first, as in:

    Code:
    int character = getchar();
    if(character != EOF) /* do something with it */
    In the current code, when you check against '\0', you're not looking at the character the user just entered, becuase you've incremented x already. What you're basically doing is this:
    Code:
    c[x] = whatever;
    x++;
    if(c[x] == '\0')
    But see how c[x] doesn't hold any value? You've not written anything there; c[x - 1] is the last character written.

    It's also not necessary in C to cast the return value of malloc(). malloc() returns void* which can be converted, without a cast, to any object pointer type, such as char*.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    Thank you so much cas! I didn't notice that problem with 'x'. I have to pay more attention while coding. Now it's working properly.

    By the way, I do have to cast the return value of malloc, otherwise I get an error when I compile the program

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    By the way, I do have to cast the return value of malloc, otherwise I get an error when I compile the program
    Then you're either using a C++ compiler or a terrible C compiler. I'd guess the former. If you're learning C, configure your compiler to be a C compiler not C++ (it might be as easy as naming the file something.c instead of something.cpp).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation and strings.
    By Sefster300 in forum C Programming
    Replies: 7
    Last Post: 01-03-2011, 03:39 PM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. Strings and Dynamic Memory
    By grooveordie in forum C Programming
    Replies: 3
    Last Post: 10-20-2008, 02:08 PM
  4. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM
  5. Ascii conversion problems with dynamic memory
    By Butters in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 12:22 PM