我想使用函数修改作为结构一部分的字符串的内容。问题是当我在函数外部打印字符串时没有输出,但是如果我在函数内部打印它,输出是 FOO,这是正确的输出。我在我认为问题所在的行中添加了一条评论。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ACCOUNT_NUM_LEN 15
#define NAME_LEN 255
#define PIN_LEN 4
#define BAL_LEN 50
typedef struct account
{
// account info
int account_num[ACCOUNT_NUM_LEN];
int pin[PIN_LEN];
float bal;
// name of the account owner
char* fname;
char* lname;
// link to next account
struct account *next;
}
account;
account* root;
int num_of_accounts;
bool init(void)
{
// example
char account_inf[80] = "FOO|BAZ|123123000012300|1234|5000.00";
const char delimiter[2] = "|";
// initialize root and set number of accounts to 0
root = NULL;
num_of_accounts = 0;
// get the first token
char* token = strtok(account_inf, delimiter);
// create a new user each line
account* new_user = malloc(sizeof(account));
if (new_user == NULL)
return false;
// initialize new user info
new_user->lname = NULL;
new_user->fname = NULL;
new_user->next = NULL;
// walk through other tokens
int info = 0;
while (token != NULL)
{
// filter info
if (info == 0)
{
new_user->lname = token; // problem
info++;
}
else if (info == 1)
{
new_user->fname = token; // problem
info++;
}
else if (info == 2)
{
for (int i = 0; i < ACCOUNT_NUM_LEN; i++)
new_user->account_num[i] = token[i] - '0';
info++;
}
else if (info == 3)
{
for (int i = 0; i < PIN_LEN; i++)
new_user->pin[i] = token[i] - '0';
info++;
}
else if (info == 4)
{
new_user->bal = atof(token);
info++;
}
token = strtok(NULL, delimiter);
}
root = new_user;
printf("%s\n", root->lname);
printf("%s\n", root->fname);
for (int i = 0; i < ACCOUNT_NUM_LEN; i++)
printf("%d", root->account_num[i]);
printf("\n");
for (int i = 0; i < PIN_LEN; i++)
printf("%d", root->pin[i]);
printf("\n");
printf("%f\n\n", root->bal);
return true;
}
int main(void)
{
// load up all accounts. exit if no account is found or made
if (!init())
return 1;
printf("%s\n", root->lname);
printf("%s\n", root->fname);
for (int i = 0; i < ACCOUNT_NUM_LEN; i++)
printf("%d", root->account_num[i]);
printf("\n");
for (int i = 0; i < PIN_LEN; i++)
printf("%d", root->pin[i]);
printf("\n");
printf("%f\n\n", root->bal);
return 0;
}