Malloc - unlimited input from user | Unix Linux Forums | Homework & Coursework Questions

  Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Malloc - unlimited input from user

Homework & Coursework Questions


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-06-2013
Joshuarodriguez Joshuarodriguez is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 21 April 2013, 11:50 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Malloc - unlimited input from user

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

I'm trying to get an unlimited input of words with an unlimited number characters from the user using
malloc and pointer to pointer then printing the inputs if only the user inputted the word "end". Even if the user
presses enter, the program should still ask input until there is no "end" inputted.
Can you please help me, I don't know if I'm doing right.



2. Relevant commands, code, scripts, algorithms:



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

int main () {

    char** pointer = malloc(sizeof(char**) * 5000);
    fgets(pointer, 5000, stdin);
    if (strncmp(pointer, "end", 5000) == 0) {
        puts(pointer);
        puts("\n");
    }
    return 0;
}



3. The attempts at a solution (include all code and scripts):



4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

AcSat QC Philippines, Mr. Jules Morcilla , CS13


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scrutinizer; 03-07-2013 at 05:09 AM.. Reason: code tags
Sponsored Links
    #2  
Old 03-07-2013
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 17 April 2014, 3:54 AM EDT
Location: Switzerland - GE
Posts: 5,490
Thanks: 148
Thanked 362 Times in 339 Posts
You can look what one of your classmates got for replies here:
http://www.unix.com/unix-dummies-que...lp-malloc.html
Sponsored Links
    #3  
Old 03-07-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 April 2014, 1:43 PM EDT
Location: Saskatchewan
Posts: 18,538
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
You're using too many *'s. Wherever you see * in front, imagine an [] at the end: ** would be an array of arrays.

sizeof(char **) doesn't make sense either. You want 5000 characters. How large is one character? sizeof(char). How large is 5000 of them? sizeof(char) * 5000.


Code:
char *buffer=(char *)malloc(sizeof(char)*5000);

Next, you don't need strncmp, strcmp() will do. fgets() and the like NULL-terminate their strings properly.

Last, how do you handle unlimited input? you can use realloc() to extend the size of an existing block of memory.


Code:
buffer=realloc(buffer, 10000*sizeof(char));

You can even call realloc() on a NULL pointer(pointer to nothing) -- it will know to just give you fresh data. So you can just run the exact same code every loop, the first time isn't anything special.

So:


Code:
int main(void)
{
        int pos=0; // pos:  how many bytes have been read already
        int size=0; // size:  how large the buffer is
        char *buffer=NULL; // The data buffer

        buffer=realloc(buffer, pos+5000); // Add 5000 more bytes.
        if(buffer == NULL)
        {
                printf("out of memory\n");
                return(1);
        }
        size=pos+5000; // We need 5000 bytes past the end of the last data
        fgets(buffer+pos, 5000, stdin); // Read it 'pos' bytes past the start of the buffer, to add to the end
        pos=strlen(buffer); // How long is the data in the buffer now?
}

    #4  
Old 03-07-2013
Joshuarodriguez Joshuarodriguez is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 21 April 2013, 11:50 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks to all, this forum really helps.
Sponsored Links
Closed Thread

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
Script interacts with user , based on user input it operates only4satish Shell Programming and Scripting 7 11-15-2012 01:12 PM
HOW to set unlimited login attempts for user in Solaris? manalisharmabe Solaris 1 07-29-2012 12:19 PM
How to get the user input recursively until the user provides valid input i.srini89 Shell Programming and Scripting 1 04-30-2012 03:25 AM
ulimit -c unlimited mr_andrew HP-UX 1 04-07-2009 11:52 AM



All times are GMT -4. The time now is 07:26 AM.