Thread: array of pointers passed as argument to fgets

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a good start.

    Two things you could do:

    1) You could enlarge the space with malloc(len + 1), and then have the '\0' included as part of the entered string.

    2) You could remove the newline that fgets() put onto the last char (before the \0) with
    Code:
    if(data[len-1]=='\n')
       data[len-1]='\0';
    And then you'd have the null termination for the string, in place. (null termination is the end of string char). Note that strcpy() will copy over the '\0', if it is present.

    Then the whole central block of code needs to go into a for loop, so you can get multiple char string space malloc'd and filled with data, instead of just str1[0].

    AND of course, if you malloc for 3 strings, you need to free() them, also in a loop.

  2. #17
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    That's a good start.

    Two things you could do:

    1) You could enlarge the space with malloc(len + 1), and then have the '\0' included as part of the entered string.

    2) You could remove the newline that fgets() put onto the last char (before the \0) with
    Code:
    if(data[len-1]=='\n')
       data[len-1]='\0';
    And then you'd have the null termination for the string, in place. (null termination is the end of string char). Note that strcpy() will copy over the '\0', if it is present.

    Then the whole central block of code needs to go into a for loop, so you can get multiple char string space malloc'd and filled with data, instead of just str1[0].

    AND of course, if you malloc for 3 strings, you need to free() them, also in a loop.
    Thanks It's working neatly when I added the given code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data type of passed argument
    By fran1942 in forum C Programming
    Replies: 1
    Last Post: 01-08-2011, 12:05 AM
  2. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. how to check if a argument passed is integer
    By powinda in forum C Programming
    Replies: 7
    Last Post: 03-07-2004, 05:26 PM
  5. could enum be passed as argument ?
    By black in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 11:49 PM