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 . . .