gpt4 book ai didi

c - 为什么会出现段错误?斯特托克?

转载 作者:行者123 更新时间:2023-11-30 21:03:14 26 4
gpt4 key购买 nike

所以我很好奇为什么下面的代码段总是出现段错误。对我来说这看起来是正确的。

int * addCoins(char *val){
const char *deli = ",";
char *ptr =NULL;

char *denomination = strtok_r(val, deli, &ptr);
char *count = strtok_r(NULL, deli, &ptr);
int deno = atoi(denomination);
int cnt = atoi(count);
int *k;
k = malloc(sizeof(*k)*2);
k[0] = deno;
k[1] =cnt;

return k;
}

调用main中的addCoins函数。我不认为问题出在这里,但老实说我对这个问题有点茫然。

char* fileNameCoin = argv[2];
FILE *fileCoin;
fileCoin = fopen(fileNameCoin, "r+");
char bufCoin[256];
int i = 0;
//vmNode->next = NULL;
int *j;
while (fgets(bufCoin, sizeof bufCoin, fileCoin) != NULL) {
j = addCoins(bufCoin);
int deno = j[0];
switch(deno){
case 5:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 10:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 20:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 50:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 100:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 200:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 500:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
case 1000:
Coins[i].denom = j[0];
Coins[i].count = j[1];
break;
default:
break;
}

i++;
}

以下是文件的定义方式

1000,3
500,4
200,20
100,30
50,5
20,3
10,40
5,20

其中第一个数字是以分为单位的面额,第二列是所述面额的数字。

这些是类型定义:

/* The different denominations of coins available */
enum denomination
{
FIVE_CENTS, TEN_CENTS, TWENTY_CENTS, FIFTY_CENTS, ONE_DOLLAR,
TWO_DOLLARS, FIVE_DOLLARS, TEN_DOLLARS
};

/* Each coin in the coins array will have a denomination (20 cents,
* 50 cents, etc) and a count - how many of that coin do we have on hand
*/
struct coin
{
enum denomination denom;
unsigned count;
};

最佳答案

老实说,您应该通过typedef更好地使用struct。示例(使用 exename in.txt 调用):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* The different denominations of coins available */
enum denomination {
FIVE_CENTS, TEN_CENTS, TWENTY_CENTS, FIFTY_CENTS, ONE_DOLLAR,
TWO_DOLLARS, FIVE_DOLLARS, TEN_DOLLARS
};

typedef struct {
enum denomination denom;
unsigned int count;
} coin;

coin * addCoins(char *val) {
coin *k = malloc(sizeof(coin));
if( sscanf( val, "%d,%d", &(k->denom), &(k->count)) != 2 ) {
fprintf(stderr,"Two int values not found on line '%s' in input.\n", val);
free(k);
k=NULL;
}
return k;
}

int main(int argc, char *argv[])
{
coin Coins[100]={0};
char* fileNameCoin = argv[1];
FILE *fileCoin = fopen(fileNameCoin, "r+");
char bufCoin[256];
int i = 0;
coin *j;
if( fileCoin ) {
while(fgets(bufCoin, sizeof bufCoin, fileCoin) != NULL) {
j = addCoins(bufCoin);
if( j ) { // only add if 2 int values found on input line
Coins[i] = *j;
free(j);
printf("c: %d, %d\n", Coins[i].denom, Coins[i].count);
i++;
}
}
fclose(fileCoin);
}
else {
fprintf(stderr,"Unable to open file %s for input.\n",fileNameCoin);
}
}

关于c - 为什么会出现段错误?斯特托克?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26219368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com