简体   繁体   English

在c中打印结构值

[英]print structure value in c

This is my code in c 这是我在C中的代码

Inside add_new_account is using scanf. 在add_new_account内部使用的是scanf。

When I print it in main function , it come out a different value that I key in. 当我在main函数中打印它时,它会输入一个不同的值。

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;
}

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. add_new_account()的成员struct方含串都悬摆指针,因为它们指向的局部变量该函数的地址。 This assigns the address of First_name to add_account.F_name : 这会将First_name的地址分配给add_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() : 使用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. 替代使用malloc()的替代方法是在struct使用固定大小的数组,因为代码已经在使用固定大小的数组进行读取。 If you choose to use malloc() remember to free() what was malloc() d. 如果选择使用malloc()请记住要free()什么是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()调用中,指定要读取的最大字符数作为格式说明符的一部分:

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. 其中最大字符数必须比要填充的数组中的最大元素数少一个,另一个元素用于scanf()写入的空终止字符。

When you are reading in a string using scanf("%s",First_name); 当您使用scanf("%s",First_name);读取字符串时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. 您可能需要使用malloc分配内存,然后使用strncpy将输入复制到指针。

Try struct like this and omit usage of local variables in the function. 像这样尝试struct并在函数中省略局部变量的使用。

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: 您还必须更改main()代码:

int main(){
    account A[20];
    int y=0;
    login();
    add_new_account(&A[y]);
    printf("First name    :");
    printf("%c\n",A[y].F_name);
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM