Thread: help me with malloc function

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    help me with malloc function

    i am new to malloc function

    help me...........
    Code:
     #include<stdio.h>
     #include<string.h>
     #include<stdlib.h>
     char * get();
     int main()
     {
       char *ptr=get();
       printf("The account number is %s",*ptr);
       return 0;
      }
      char * get() //getting the account number. 
      {
        char acc[10],*ptr1;
        int len;
        printf("Entr the account number\n");
        gets(acc);
        len=strlen(acc);
        *ptr1 =(char*)malloc((len+1)*sizeof(char));
        return(ptr1);
      }
    "acc2.c", line 19: warning: improper pointer/integer combination: op "="

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    See my comments inline
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char * get(void); /* In C no arguments and empty argument list is not the same */
    
    int main(void)
    {
        char *ptr=get();
        printf("The account number is <%s>\n",ptr); /* %s expets pointer to cahr */
    
        free(ptr); /* you need to free what was malloced after you do not need it anymore */
        return 0;
    }
    char * get(void) //getting the account number.
    {
        char acc[10],*ptr1;
        int len;
        printf("Enter the account number\n");
        if(fgets(acc, sizeof (acc), stdin) == NULL) /* do not use gets - read FAQ */
        {
            /* input failed */
            return NULL;
        }
        len=strlen(acc);
        if(len > 0 && acc[len-1] == '\n')
        {
            acc[len-1] = 0;
        }
        ptr1 = malloc((len+1)*sizeof(char)); /* In C you do not cast malloc */
        if(ptr1)
        {
            strcpy(ptr1,acc); /* you forgot to copy what was read into the allocated memory */
        }
        return ptr1;
    }
    And here is the result

    Code:
    Enter the account number
    12345678
    The account number is <12345678>
    Press any key to continue . . .
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    thank you so much...........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't malloc from within a function
    By synthetix in forum C Programming
    Replies: 5
    Last Post: 11-02-2011, 12:28 PM
  2. malloc in function
    By mugen in forum C Programming
    Replies: 13
    Last Post: 03-14-2010, 11:27 AM
  3. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  4. Help with Malloc() function
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 09-19-2007, 03:17 PM
  5. about malloc function
    By enes in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 09:33 AM