Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 值返回错误的值_C_Function_Pointers_Return - Fatal编程技术网

C 值返回错误的值

C 值返回错误的值,c,function,pointers,return,C,Function,Pointers,Return,我正在创建一个函数,该函数获取个人记录(存储在函数参数中的*individual),分离记录以获取个人生日,然后该函数计算他们的年龄。我遇到的问题是,当我使用strrchr搜索生日记录时,当我不希望它更改原始值时,它会更改原始值。因此,它不是像应该的那样存储Steven,Cortright,3/1/1940,而是存储Steven,Cortright,3 我想尽一切办法来解决这个问题。以下是我的代码,非常感谢您的帮助/建议: char* calcage(char *individual) {

我正在创建一个函数,该函数获取个人记录(存储在函数参数中的
*individual
),分离记录以获取个人生日,然后该函数计算他们的年龄。我遇到的问题是,当我使用
strrchr
搜索生日记录时,当我不希望它更改原始值时,它会更改原始值。因此,它不是像应该的那样存储
Steven,Cortright,3/1/1940
,而是存储
Steven,Cortright,3

我想尽一切办法来解决这个问题。以下是我的代码,非常感谢您的帮助/建议:

char* calcage(char *individual)
  {

    time_t current_time;
    char *c_time_string;
    char *birthday;
    char *bmonth, *bday, *byear;
    int numbmonth, numbday,  numbyear;
    struct tm str_bday;
    time_t time_bday;
    double diff;
    double years;

    double monthscalc;
    int monthsage;
    int yearsage;


    current_time = time(NULL);


    c_time_string = ctime(&current_time);
    birthday = strrchr(individual, ',');
    birthday++;


    bmonth = strtok(birthday, "/");
    bday = strtok(NULL, "/");
    byear = strtok(NULL, "/");

    numbmonth = atoi(bmonth);
    numbday = atoi(bday);
    numbyear = atoi(byear);

    str_bday.tm_year = numbyear - 1900;
    str_bday.tm_mon = numbmonth - 1;
    str_bday.tm_mday = numbday;
    str_bday.tm_hour = 0;
    str_bday.tm_min = 0;
    str_bday.tm_sec = 1;
    time_bday = mktime(&str_bday);

    diff = difftime(current_time, time_bday);
    years = diff / 60 / 60 / 24 / 365.242;

    yearsage = (int) years;


    int inpart = (int) years;
    double months = years - inpart;
    monthscalc = (365.242 * months) / 30.4368;

    monthsage = (int) monthscalc;

    char *calculatedAge = (char *)malloc(50*sizeof(char));
    snprintf(calculatedAge,100,  "You are %d years and %d months old.", yearsage, \
    monthsage);

    return calculatedAge;
  }

问题是strtok,而不是strrchr
strtok
将空字节放在原始缓冲区中的令牌后。

问题是
strtok
,而不是
strrchr
strtok
将空字节放在原始缓冲区中的令牌后。

问题是
strtok
,而不是
strrchr
strtok
将空字节放在原始缓冲区中的令牌后。

问题是
strtok
,而不是
strrchr
strtok
将空字节放在原始缓冲区中的令牌后。

使用sscanf代替strtok来解析字符串:

int month = 0, day = 0, year = 0;
sscanf( birthday, "%d/%d/%d", &month, &day, &year );

这样,您就不会弄乱参数,而不用strtok使用sscanf解析字符串:

int month = 0, day = 0, year = 0;
sscanf( birthday, "%d/%d/%d", &month, &day, &year );
char* calcage(char *individual){
    time_t now = time(NULL);
    struct tm *local = localtime(&now);

    char *temp = strdup(strrchr(individual, ',')+1);//make copy
    char *birthday = temp;
    int bmonth, bday, byear;
    bmonth = atoi(strtok(temp, "/"));
    bday   = atoi(strtok(NULL, "/"));
    byear   = atoi(strtok(NULL, "/"));
    int age = local->tm_year + 1900 - byear -1;
    int lm = local->tm_mon + 1;
    if(bmonth <= lm && bday <= local->tm_mday)
        ++age;
    int mtemp = lm - bmonth;
    if(mtemp < 0 || mtemp == 0 && bday > local->tm_mday)
        mtemp += 12;
    int mage = age * 12 + mtemp;
    char *calculatedAge = realloc(temp, 50);
    snprintf(calculatedAge, 50, "You are %d years and %d months old.", age, mage);
    return calculatedAge;
}

这样,您就不会弄乱参数,而不用strtok使用sscanf解析字符串:

int month = 0, day = 0, year = 0;
sscanf( birthday, "%d/%d/%d", &month, &day, &year );
char* calcage(char *individual){
    time_t now = time(NULL);
    struct tm *local = localtime(&now);

    char *temp = strdup(strrchr(individual, ',')+1);//make copy
    char *birthday = temp;
    int bmonth, bday, byear;
    bmonth = atoi(strtok(temp, "/"));
    bday   = atoi(strtok(NULL, "/"));
    byear   = atoi(strtok(NULL, "/"));
    int age = local->tm_year + 1900 - byear -1;
    int lm = local->tm_mon + 1;
    if(bmonth <= lm && bday <= local->tm_mday)
        ++age;
    int mtemp = lm - bmonth;
    if(mtemp < 0 || mtemp == 0 && bday > local->tm_mday)
        mtemp += 12;
    int mage = age * 12 + mtemp;
    char *calculatedAge = realloc(temp, 50);
    snprintf(calculatedAge, 50, "You are %d years and %d months old.", age, mage);
    return calculatedAge;
}

这样,您就不会弄乱参数,而不用strtok使用sscanf解析字符串:

int month = 0, day = 0, year = 0;
sscanf( birthday, "%d/%d/%d", &month, &day, &year );
char* calcage(char *individual){
    time_t now = time(NULL);
    struct tm *local = localtime(&now);

    char *temp = strdup(strrchr(individual, ',')+1);//make copy
    char *birthday = temp;
    int bmonth, bday, byear;
    bmonth = atoi(strtok(temp, "/"));
    bday   = atoi(strtok(NULL, "/"));
    byear   = atoi(strtok(NULL, "/"));
    int age = local->tm_year + 1900 - byear -1;
    int lm = local->tm_mon + 1;
    if(bmonth <= lm && bday <= local->tm_mday)
        ++age;
    int mtemp = lm - bmonth;
    if(mtemp < 0 || mtemp == 0 && bday > local->tm_mday)
        mtemp += 12;
    int mage = age * 12 + mtemp;
    char *calculatedAge = realloc(temp, 50);
    snprintf(calculatedAge, 50, "You are %d years and %d months old.", age, mage);
    return calculatedAge;
}
这样就不会弄乱参数

char*calage(char*individual){
char* calcage(char *individual){
    time_t now = time(NULL);
    struct tm *local = localtime(&now);

    char *temp = strdup(strrchr(individual, ',')+1);//make copy
    char *birthday = temp;
    int bmonth, bday, byear;
    bmonth = atoi(strtok(temp, "/"));
    bday   = atoi(strtok(NULL, "/"));
    byear   = atoi(strtok(NULL, "/"));
    int age = local->tm_year + 1900 - byear -1;
    int lm = local->tm_mon + 1;
    if(bmonth <= lm && bday <= local->tm_mday)
        ++age;
    int mtemp = lm - bmonth;
    if(mtemp < 0 || mtemp == 0 && bday > local->tm_mday)
        mtemp += 12;
    int mage = age * 12 + mtemp;
    char *calculatedAge = realloc(temp, 50);
    snprintf(calculatedAge, 50, "You are %d years and %d months old.", age, mage);
    return calculatedAge;
}
time\u t now=时间(空); struct tm*local=localtime(&now); char*temp=strdup(strrchr(单个“,”)+1);//复制 char*生日=临时工; 国际b月,b日,b月; b月=atoi(标准温度“/”); bday=atoi(strtok(NULL,“/”); byear=atoi(strtok(NULL,“/”); int age=local->tm_year+1900-byear-1; int lm=local->tm\u mon+1; 如果(b月本地->tm\mday) mtemp+=12; int mage=年龄*12+mtemp; char*calculatedAge=realloc(温度,50); snprintf(计算年龄,50,“你有%d岁和%d个月大。”,年龄,法师); 返回计算日期; }
char*calage(char*individual){
time\u t now=时间(空);
struct tm*local=localtime(&now);
char*temp=strdup(strrchr(单个“,”)+1);//复制
char*生日=临时工;
国际b月,b日,b月;
b月=atoi(标准温度“/”);
bday=atoi(strtok(NULL,“/”);
byear=atoi(strtok(NULL,“/”);
int age=local->tm_year+1900-byear-1;
int lm=local->tm\u mon+1;
如果(b月本地->tm\mday)
mtemp+=12;
int mage=年龄*12+mtemp;
char*calculatedAge=realloc(温度,50);
snprintf(计算年龄,50,“你有%d岁和%d个月大。”,年龄,法师);
返回计算日期;
}
char*calage(char*individual){
time\u t now=时间(空);
struct tm*local=localtime(&now);
char*temp=strdup(strrchr(单个“,”)+1);//复制
char*生日=临时工;
国际b月,b日,b月;
b月=atoi(标准温度“/”);
bday=atoi(strtok(NULL,“/”);
byear=atoi(strtok(NULL,“/”);
int age=local->tm_year+1900-byear-1;
int lm=local->tm\u mon+1;
如果(b月本地->tm\mday)
mtemp+=12;
int mage=年龄*12+mtemp;
char*calculatedAge=realloc(温度,50);
snprintf(计算年龄,50,“你有%d岁和%d个月大。”,年龄,法师);
返回计算日期;
}
char*calage(char*individual){
time\u t now=时间(空);
struct tm*local=localtime(&now);
char*temp=strdup(strrchr(单个“,”)+1);//复制
char*生日=临时工;
国际b月,b日,b月;
b月=atoi(标准温度“/”);
bday=atoi(strtok(NULL,“/”);
byear=atoi(strtok(NULL,“/”);
int age=local->tm_year+1900-byear-1;
int lm=local->tm\u mon+1;
如果(b月本地->tm\mday)
mtemp+=12;
int mage=年龄*12+mtemp;
char*calculatedAge=realloc(温度,50);
snprintf(计算年龄,50,“你有%d岁和%d个月大。”,年龄,法师);
返回计算日期;
}

这是因为strok按照文档中的描述修改了它的参数。这是因为strok按照文档中的描述修改了它的参数。这是因为strok按照文档中的描述修改了它的参数。这是因为strok按照文档中的描述修改了它的参数。谢谢。当我在这里发布之前试图复制它时,我尝试使用strcpy,不知道strdup。谢谢你的帮助非常感谢。当我在这里发布之前试图复制它时,我尝试使用strcpy,不知道strdup。谢谢你的帮助非常感谢。当我在这里发布之前试图复制它时,我尝试使用strcpy,不知道strdup。谢谢你的帮助非常感谢。当我在这里发布之前试图复制它时,我尝试使用strcpy,不知道strdup。谢谢你的帮助