I am trying to understand how to return and pass the char pointer between the funtions.
My sample code is:
Code:
#include<stdio.h>
#include<string.h>
char* input();
void display(char*);
int main()
{
char *acc_no=input();
printf("The account number is from outside the input function %s \n",acc_no);
display(acc_no);
return 0;
}
char* input()
{
char *acc_no=NULL;
char acc_num[10];
printf("enter the account_no \n");
fgets(acc_num,10,stdin);
printf("The account number is %s \n",acc_num);
acc_no = acc_num;
printf("The account number is from the pointer %s \n",acc_no);
return (acc_no);
}
void display(char *acc_no)
{
printf("The account nummber is %s \n",acc_no);
}
The output is
enter the account_no
12313
The account number is 12313
The account number is from the pointer 12313
The account number is from outside the input function 12313
The account nummber is ÿÂ
The funtion input is returning char pointer correctly.
But while passing char pointer to the function display, it is passing some garbage value....
pls explain me what is the mistake....
Thanks in advance.