|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
|
| The Following User Says Thank You to shamrock For This Useful Post: | ||
eracav (03-05-2013) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Oh. Thank you very much.
|
|
#4
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Thank you very much to all of you.
|
| Sponsored Links | ||
|
![]() |
| Tags |
| fgets, malloc |
| Thread Tools | Search this Thread |
| 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 |
|
|