Friday 15 April 2011

c - Why is this segfaulting? strtok? -



c - Why is this segfaulting? strtok? -

so curious why next segment of code keeps segfaulting. looks right me.

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; homecoming k; }

the phone call function addcoins in main. not think fault lies in here @ bit of loss problem.

char* filenamecoin = argv[2]; file *filecoin; filecoin = fopen(filenamecoin, "r+"); char bufcoin[256]; int = 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++; }

the next how file defined

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

where first number denomination in cents, , 2nd column number of said denomination.

these typedefs:

/* 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 coins array have denomination (20 cents, * 50 cents, etc) , count - how many of coin have on hand */ struct coin { enum denomination denom; unsigned count; };

honestly, should using structs improve via typedef. illustration (invoke exename in.txt):

#include <stdio.h> #include <stdlib.h> #include <string.h> /* 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; } homecoming k; } int main(int argc, char *argv[]) { coin coins[100]={0}; char* filenamecoin = argv[1]; file *filecoin = fopen(filenamecoin, "r+"); char bufcoin[256]; int = 0; coin *j; if( filecoin ) { while(fgets(bufcoin, sizeof bufcoin, filecoin) != null) { j = addcoins(bufcoin); if( j ) { // add together 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 open file %s input.\n",filenamecoin); } }

c segmentation-fault strtok

No comments:

Post a Comment