Reputation: 43
This is my code in c
Inside add_new_account is using scanf.
When I print it in main function , it come out a different value that I key in.
Can someone help me to solve this problem please.
Thanks for helping
struct account
{
char* F_name;
char* L_name;
int IC_No;
char* address;
char* e_address;
int c_number;
};
void add_new_account(struct account A[]){
int y=0;
char First_name[20],Last_name[20],addres[20],email[20];
int IC,number;
struct account add_account;
printf("First name :");
scanf("%s",First_name);
add_account.F_name=First_name;
printf("Last name :");
scanf("%s",Last_name);
add_account.L_name = Last_name;
printf("IC No. :");
scanf("%d",&IC);
add_account.IC_No = IC;
printf("Address :");
scanf("%s",addres);
add_account.address = addres;
printf("Email address :");
scanf("%s",email);
add_account.e_address = email;
printf("Contact number:");
scanf("%d",&number);
add_account.c_number = number;
A[y] = add_account;
}
int main(){
struct account A[20];
int y=0;
login();
add_new_account(&A);
printf("First name :");
printf("%c\n",A[y].F_name);
printf("Last name :");
printf("%s\n",A[y].L_name);
printf("IC No. :");
printf("%d\n",A[y].IC_No);
printf("Address :");
printf("%s\n",A[y].address);
printf("Email address :");
printf("%s\n",A[y].e_address);
printf("Contact number:");
printf("%d\n",A[y].c_number);
scanf("%d",&y);
return 0;
}
Upvotes: 0
Views: 852
Reputation: 3029
Try struct like this and omit usage of local variables in the function.
typedef struct
{
char F_name[20];
char L_name[20];
int IC_No;
char address[20];
char e_address[20];
int c_number;
} account;
void add_new_account(account *A)
{
printf("First name :");
scanf("%s", A->F_name);
...
Doesn't it look better and more readable?
You must also change the main() code:
int main(){
account A[20];
int y=0;
login();
add_new_account(&A[y]);
printf("First name :");
printf("%c\n",A[y].F_name);
...
Upvotes: 1
Reputation: 122001
Outside of add_new_account()
the members of the struct
containg strings are all dangling pointers as they are pointing to the addresses of variables local to that function. This assigns the address of First_name
toadd_account.F_name
:
add_account.F_name=First_name;
it does perform a copy. You need to copy the content of the local variables to make them available outside of the function. Example using strcpy()
:
add_account.F_name = malloc(strlen(First_name) + 1);
if (add_account.F_name)
{
strcpy(add_account.F_name);
}
Instead of using malloc()
an alternative would be to use fixed sized arrays in the struct
, as the code is already using fixed sized arrays for reading. If you choose to use malloc()
remember to free()
what was malloc()
d.
Also, to prevent buffer overrun specify the maximum number of characters to read as part of the format specifier in the scanf()
calls:
scanf("%19s", First_name);
where the maximum number of characters must be one less than the maximum number of elements in the array being populated, the other element is used for the null terminating character which scanf()
writes.
Upvotes: 4
Reputation: 13217
When you are reading in a string using scanf("%s",First_name);
for example, you are reading the value into the array you assigned on the stack in your function.
Afterwards, you are assigning
add_account.F_name=First_name;
by this, you are assigning the address of the array from the stack to your pointer. When your function returns, this address and the value will be lost. You would need to either allocate memory using malloc
and then use strncpy
to copy the input to your pointer.
Upvotes: 1