c - functions to append and read from linked lists -


i working on small test program can perform 2 functions on linked list:

  1. append linked list.
  2. get appended.

so wrote 2 functions can perform that. doesn't work. if call function append_work() prints out appended work , correct values passed. afterwards doesn't seem correctly set pointer next struct. don`t know why happening.

a similiar thing happens when call get_work() prints out current work correctly doesn't set next one. outptr there returns struct on work done while stil settings work_travel next one!

i missing obvious pointers not seeing it...

here code. compiles:

#define          _posix_c_source                             200809l   #define          errno_bufsize                               256  #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h>  int strerror_r(int errnum, char *buf, size_t buflen);  typedef struct work work;  struct work {      char                *str;     int                 i;     work                *next;  };  int append_work(work *ptr, char *work, int i) {     char    strerror_buf[errno_bufsize];      ptr->next = malloc(sizeof(work));     if (ptr == null) {         strerror_r(errno, strerror_buf, errno_bufsize);         printf("malloc() err: %s\n",strerror_buf);         return -1;     }      ptr = ptr->next;     ptr->str=work;     ptr->i=i;     printf("appending work: %s\n",ptr->str);      ptr->next = null;      return 0;  }  int get_work(work *inptr, work *outptr) {      if (inptr == null) {         printf(" no work found...\n");         return -1;     } else {         outptr = inptr;         printf(" work found: str: %s|| int: %d\n",inptr->str, inptr->i);         inptr = inptr->next;         return 0;     } }    int main() {      char                strerror_buf[errno_bufsize];      work                *root;     work                *work_travel;     work                *add_work;     work                *curr_work;      root = malloc(sizeof(work));     if (root == null) {         strerror_r(errno, strerror_buf, errno_bufsize);         printf("malloc() err: %s\n",strerror_buf);         exit(1);     }     root->str="work 0";     root->i=0;       work_travel = root;     add_work = root;      append_work(add_work, "work 1", 1);     append_work(add_work, "work 2", 2);     append_work(add_work, "work 3", 3);     append_work(add_work, "work 4", 4);     append_work(add_work, "work 5", 5);     append_work(add_work, "work 6", 6);     append_work(add_work, "work 7", 7);     append_work(add_work, "work 8", 8);     append_work(add_work, "work 9", 9);      get_work(work_travel, curr_work);     get_work(work_travel, curr_work);     get_work(work_travel, curr_work);      exit(1);  } 

first of all, having "struct work", type named "work", , variable named "work" really bad programming style.

that said, problem inptr local variable in get_work, changing not change work_travel when call get_work(work_travel, curr_work). have return updated value somehow, either return value get_work (though solution loose success flag, not use :-) ):

work *get_work (work *inptr, work *outptr) {    ...    return inptr; } 

and call like:

work_travel = get_work(work_travel, curr_travel); 

or pass in pointer to-be-updated object like:

int get_work (work **inptr_p, work *outptr) {   work *inptr = *inptr_p;   if (inptr == null) {     printf(" no work found...\n");     return -1;   } else {     outptr = inptr;     printf(" work found: str: %s|| int: %d\n",inptr->str, inptr->i);     inptr = inptr->next;     *inptr_p = inptr;     return 0;   } } ... get_work (&work_travel, curr_travel); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -