[C] malloc and fgets

This is a discussion on [C] malloc and fgets within the C Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <string.h> #include <stdio.h> void memoryAlloc (int a); int main(int argc, char* argv[]) { int choice; fputs("Please ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    [C] malloc and fgets

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    void memoryAlloc (int a);
    
    int main(int argc, char* argv[])
    {
    	int choice;
    	fputs("Please enter the maximum length of string: ",stdout);
    	scanf("%d",&choice);
    	memoryAlloc(choice);
    	
    	return 0;
    }
    
    void memoryAlloc (int a){
    
    	char* arr;
    	
    	arr = (char*)malloc( a * sizeof(char));
    	if (arr == NULL){
    		fputs("Failed to allocate memory!!!",stdout);
    		exit(1);
    	}
    	
    	fputs("Please enter strings: ",stdout);
    	fgets(arr,sizeof(arr),stdin); // scanf("%s",arr);
    
    	printf("Entered strings: %s\n",arr);
    
    	fflush(stdin); 
    	free(arr);
    }
    Hello, Ive started studying C for fun, and got a problem studying dynamic memry allocation part.
    I wrote this code to understand how to use malloc.
    I wanted to get a string from a user and then print a result.
    I really don't understand why my code doesnt allow to type strings.
    If I use scanf() instead of fgets, it works fine. Anyone got an idea why it happens??!!
    Last edited by baraq; 11-04-2010 at 10:15 PM. Reason: clearfying my question

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Last edited by Bayint Naung; 11-04-2010 at 10:24 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    The other problem you have is mixing scanf() with fgets()

    > scanf("%d",&choice);
    This leaves trailing characters (specifically a newline) on the input stream

    > fgets(arr,sizeof(arr),stdin);
    This reads the remainder of the stream, up to the first newline. Typically, this is the only character.

    In response to your first prompt, try typing in something like
    123!?

    > printf("Entered strings: %s\n",arr);
    This would print the !?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets, fputs, and malloc.
    By eXeCuTeR in forum C Programming
    Replies: 41
    Last Post: 11-30-2007, 07:09 AM
  2. Reset fgets to read from the beginning of a file?
    By bivhitscar in forum C Programming
    Replies: 2
    Last Post: 05-21-2006, 12:42 AM
  3. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 10:29 PM
  4. malloc
    By powerboy in forum C Programming
    Replies: 11
    Last Post: 05-25-2003, 10:26 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

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