question about the fgets()

This is a discussion on question about the fgets() within the C Programming forums, part of the General Programming Boards category; There is another question I want to ask about the fgets() in C. Here is my code Code: #include <string.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    Post question about the fgets()

    There is another question I want to ask about the fgets() in C. Here is my code
    Code:
    #include <string.h>
    #include <stdio.h>
    1.int main (int argc, char *argv[]){
    2.	char* input;
    3.	char* arg;
    4.	fgets ( input ,43, stdin ) ;
    5.     arg = strtok(input," ");          
    6.	printf("%s\n",arg);
    7.}
    After compiling and executing the code, I got the error
    Code:
    red 313 % input.out
    input from the screen
    Segmentation fault
    I thought it is gonna print out
    Code:
    input
    from
    the
    screen
    but it did not. I need an explanation for it

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by thungmail View Post
    There is another question I want to ask about the fgets() in C. Here is my code

    Code:
    2.	char* input;

    Here you are declaring a pointer to char. It's 4 bytes.


    Code:
    4.	fgets ( input ,43, stdin ) ;

    Here you are writing 43 bytes to that memory location.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Code:
    4.	fgets ( input ,43, stdin ) ;
    I declare 43 bytes because i want to make sure that there is enough for the string from stdin. If i made a small change at line 2
    Code:
    char input[43];
    It will work. However, i dont want to make a change at line 2. what should i implement the code

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What do you mean? You don't have 43 bytes.

    You have to set aside memory for your string either by using the array notation like you did above or using malloc.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Well you can do that, but further on down the road, you will SIGFAULT. You must allocate enough space for your string that fgets() will place there. Using fgets() is a much safer mechanism to place a string but you must use the protections there of and not exceed your bounds for the string in use.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    ok now i am using malloc
    Code:
    input = (char*)( malloc(sizeof(char))
    Now the problem is on malloc(sizeof(char) because it does not make sure the bounds of the input unless we have to declare a length for the input at the beginning

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Well there are a couple of ways to accomplish sitting aside memory for this. One was given to you already with the array declaration. With malloc you need to specify how many of this data type you need room for. You have only specified room for only on char.

    input = malloc (43 * sizeof (char));

    Note: input will need to be a pointer to char. No need to cast your malloc return type either.

    Also when ever using malloc/calloc/realloc, always check for proper return values.
    Last edited by slingerland3g; 11-05-2009 at 11:21 AM. Reason: Note to check returne type

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >input = malloc (43 * sizeof (char));
    Also note that sizeof(char) is absolutely guaranteed to evaluate to 1. Thus you can do this to the same effect:
    Code:
    input = malloc(43);
    A better option across the board is to future-proof for when you want to move to another type:
    Code:
    input = malloc(43 * sizeof *input);
    Now the call to malloc is dependent on the type of the pointer rather than independently specified. This final line won't need to change at all if the type of input switches from char* to wchar_t*, for example. This is especially important for strings because more and more code is becoming language-aware and moving outside of the basic character set.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Thanks for your anwsers. I am clear now

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by Prelude View Post
    >input = malloc (43 * sizeof (char));
    Also note that sizeof(char) is absolutely guaranteed to evaluate to 1. Thus you can do this to the same effect:
    Code:
    input = malloc(43);
    A better option across the board is to future-proof for when you want to move to another type:
    Code:
    input = malloc(43 * sizeof *input);
    Now the call to malloc is dependent on the type of the pointer rather than independently specified. This final line won't need to change at all if the type of input switches from char* to wchar_t*, for example. This is especially important for strings because more and more code is becoming language-aware and moving outside of the basic character set.

    Yes much better!

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets question
    By mattyg in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 03:25 AM
  2. Question on using fgets
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 10-17-2008, 10:23 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. fgets problem (i think)
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 10:19 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21