作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我很好奇为什么下面的代码段总是出现段错误。对我来说这看起来是正确的。
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/
我是一名优秀的程序员,十分优秀!