Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-05-2013
Registered User
 
Join Date: Feb 2013
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Help with malloc()

Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you.

Here's my code:

Code:
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*));
    fgets(pointer, malloc(sizeof(char*)), stdin);
    puts(pointer);


    return 0;

}

I get the warning: passing of argument 2 of 'fgets' makes integer from pointer without a cast

What does that mean? Thank you very much
Sponsored Links
    #2  
Old 03-05-2013
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,303
Thanks: 11
Thanked 99 Times in 95 Posts
Quote:
Originally Posted by eracav View Post
Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you.

Here's my code:

Code:
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*));
    fgets(pointer, malloc(sizeof(char*)), stdin);
    puts(pointer);


    return 0;

}

I get the warning: passing of argument 2 of 'fgets' makes integer from pointer without a cast

What does that mean? Thank you very much
Because the second argument of fgets is an integer while malloc returns pointer to void so take it out...
The Following User Says Thank You to shamrock For This Useful Post:
eracav (03-05-2013)
Sponsored Links
    #3  
Old 03-05-2013
Registered User
 
Join Date: Feb 2013
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Oh. Thank you very much.
    #4  
Old 03-05-2013
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,468
Thanks: 62
Thanked 528 Times in 462 Posts
Quote:
Originally Posted by shamrock View Post
Because the second argument of fgets is an integer while malloc returns pointer to void so take it out...
The problem is much deeper than that.

The given program allocates enough space to hold a pointer to a character; not enough space to read a line of user input. And, I don't see how eracav can preallocate space for "unlimited input from the user". In addition to that, eracav isn't checking the return value of malloc() to determine if space was allocated nor of fgets() to determine if an error or EOF has been encountered before a <newline> was found.

To do what needs to be done, eracav will need much more complicated logic with a loop calling fgets() and realloc() to increase the input buffer size until a <newline> or EOF has been copied into the growing buffer.

Is this a homework assignment?
The Following User Says Thank You to Don Cragun For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #5  
Old 03-05-2013
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,657
Thanks: 164
Thanked 645 Times in 622 Posts

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*) * 4096);
    if(pointer==NULL)
    {
        perror("Cannot allocate memory");
        exit(1);
    }
    fgets(pointer, 4096, stdin);
    puts(pointer);
    puts("\n");   // in case the user enters more than 4095 chars and you lose the trailing \n
    free(pointer);
    return 0;
}

You have to set an arbitrary limit, I chose 4096.
Best practice if you malloc:
1. check the return code from the malloc call, and handle any errors.
2. free any memory you do not need anymore.

And. You code did not work, it had undefined behavior. A successful compile in C means no warnings, no errors, no dereference of undefined or possibly NULL pointers.
The Following User Says Thank You to jim mcnamara For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #6  
Old 03-05-2013
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,380
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
char * is 'pointer to characters', you don't want to allocate pointers, you want to allocate characters. So, sizeof(char) would make more sense than sizeof(char *).
The Following User Says Thank You to Corona688 For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #7  
Old 03-06-2013
Registered User
 
Join Date: Feb 2013
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Thank you very much to all of you.
Sponsored Links
Closed Thread

Tags
fgets, malloc

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Malloc Implementation in C rbansal2 UNIX for Advanced & Expert Users 2 02-27-2010 08:05 PM
malloc() rash123 Programming 3 08-28-2008 11:35 AM
When to use Malloc? Tonje Programming 5 10-17-2005 10:14 AM
malloc function rajashekaran Programming 7 09-30-2002 01:27 PM
malloc rajashekaran Programming 2 09-30-2002 05:24 AM



All times are GMT -4. The time now is 01:25 PM.