當我在微控制器上運行此代碼時,嘗試在“price_right_of_period”上列印 printf 時它會崩潰。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DEBUG
char *str = "$3.45";
int main()
{
char *decimal_pos; // where the decimal place is in the string
char buffer[64]; // temporary working buffer
int half_price_auto_calc = 1;
// the cost of the product price difference for half price mode
int price_save_left, price_save_right = 0;
int price_half_left, price_half_right = 0;
// EG: let's say we get the string "$2.89"
char *price_left_of_period; // this will contain "2"
char *price_right_of_period; // this will contain "89"
// find where the decimal is
decimal_pos = strstr(str, ".");
if (decimal_pos != NULL)
{
printf("\nThe decimal point was found at array index %d\n", decimal_pos - str);
printf("Splitting the string \"%s\" into left and right segments\n\n", str);
}
// get everything before the period
strcpy(buffer, str); // copy the string
price_left_of_period = strtok(buffer, ".");
// if the dollar sign exists, skip over it
if (price_left_of_period[0] == '$') price_left_of_period ;
#ifdef DEBUG
printf("price_left_of_period = \"%s\"\n", price_left_of_period);
#endif
// get everything after the period
//
// strtok remembers the last string it worked with and where it ended
// to get the next string, call it again with NULL as the first argument
price_right_of_period = strtok(NULL, "");
#ifdef DEBUG
printf("price_right_of_period = \"%s\"\n\n", price_right_of_period);
#endif
if (half_price_auto_calc == 1)
{
// calculate the amount we saved (before the decimal)
price_save_left = atoi((const char *)price_left_of_period);
// halve the value if half price value is true
price_half_left = price_save_left / 2;
// calculate the amount we saved (before the decimal)
price_save_right = atoi((const char *)price_right_of_period);
// halve the value if half price value is true
price_half_right = price_save_right / 2;
#ifdef DEBUG
printf("price_half_left = \"%d\"\n", price_half_left);
printf("price_half_right = \"%d\"", price_half_right);
#endif
}
return 0;
}
代碼在此處運行并正常作業:
有沒有人知道為什么我的代碼可能會發生這種情況?代碼在我看來是正確的,但從其他 C 專家那里獲得第二意見總是很好:)
解決方案:
您的代碼確實有一個錯誤。
%d\n", decimal_pos - str不起作用,因為decimal_pos - str通過 %d 列印的型別錯誤。您需要投射它(我懷疑這會導致崩潰,但您可以通過將其注釋掉并重新測驗來進行測驗)
uj5u.com熱心網友回復:
這里的代碼確實有一個錯誤:
printf("\nThe decimal point was found at array index %d\n", decimal_pos - str);
2 個指標的不同型別ptrdiff_t可能與int預期的不同%d。您應該使用%td或將差異轉換為(int)(decimal_pos - str). 然而,令人驚訝的是,這種型別的不匹配是導致您出現問題的原因。
請注意,您復制字串時沒有測驗其長度,在strcpy(buffer, str);本示例中是可以的,但如果str指向更長的字串,則可能會出現未定義的行為。
代碼太復雜:沒有必要,strtok()因為您已經有了小數點的偏移量(如果有的話)。您可以使用atoi()指向整數部分開頭的指標,而不用.空位元組修補。你也可以strtol()用來避免strstr()。
另請注意,在大多數情況下,代碼將計算出錯誤的價格:"$3.45"將更改為"$1.22"大大超過 50% 的回扣。
您應該將數字轉換為美分的整數,然后使用它來計算降低的價格。
這是一個簡化版本:
#include <stdio.h>
#include <stdlib.h>
int half_price_auto_calc = 1;
char *str = "$3.45";
int main() {
int cents;
char *p = str;
if (*p == '$')
p ;
// use strtol to convert dollars and cents
cents = strtol(p, &p, 10) * 100;
if (*p == '.') {
cents = strtol(p 1, NULL, 10);
}
if (half_price_auto_calc) {
cents /= 2;
}
printf("reduced price: $%d.d\n", cents / 100, cents % 100);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/400290.html
上一篇:無法洗掉我的鏈表的頭節點
下一篇:c中的不透明資料型別和結構
