c - Fill value by value a char * -
i'm trying fill char *info
inside struct nodo;
this struct :
struct nodo { char *info; struct nodo *prec; struct nodo *succ; }; typedef struct nodo nodo;
and whole function :
nodo *q,*t = null,*head = null; int i, nodi=0 ; char *c = a, *k = a; while ( *c != '\0') { if (*c == ' ') nodi++; c++; } (i = 0; nodi != 0; i++) { if (i == 0){ head = createfirstnodo(); t = head;} if (i == 1) q = createsecondnodo(head); else q = creatennodo(head); nodi--; } char *k = a; int = 0; while ( *k != '\0' ) { if (*k == ' ' ) { head = head->succ; = 0; } head->info[i] = *k; // error exc bad access i++; k++; } return t; }
k
char*
, should scroll char[]
assigned , should copy of values proper place in head->info[i]
char *info
in struct
if k
hits space, nodo goes next nodo , 'i' become 0 again since i
need word filled in each nodo.
but code shows error exc bad access in marked line.
here 3 createnodo functions:
nodo *createfirstnodo() { nodo *q; q = malloc(sizeof(nodo)); q->prec = null; q->succ = null; return q; } nodo *createsecondnodo(nodo *head) { nodo *q; q = malloc(sizeof(nodo)); q->succ = null; q->prec = head; head->succ = q; return q; } nodo *creatennodo(nodo *head) { nodo *q; while (head->succ != null) { head = head ->succ; } q = malloc(sizeof(nodo)); q->succ = null; q->prec = head; head->succ = q; return q;
edit: sorry lacking code.
the reason exc bad access because never allocate memory info
. in struct nodo
have info
defined pointer character, never have memory allocated it.
depending on how big input could either char info[256]
(or whatever size want) or calloc
amount of space want in createxxxx
functions.
additionally, need 1 create
function. this:
nodo *createnodo(nodo *head) { nodo *q; q = calloc(1, sizeof(nodo)); if (q) { //assume info should 256 chars q->info = calloc(256, sizeof(char)); if (!q->info) { free(q); return null; } q->succ = null; q->prec = null; if (head) { while (head->succ != null) { head = head->succ; } q->prec = head; head->succ = q; } return (q); }
that way have 1 function debug/fix.
Comments
Post a Comment