Thread: Passing Arguments

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    17

    Question Passing Arguments

    I am working on a linked list using separate files. I have a working linked list where all my functions are in one file but when I try to compile using separate files I have issues. I don't understand what changed with the pointers. Or am I not including things correctly, also prior to separate files I didn't use a header file or function prototypes, but I believe I have those correct.


    prog4.c: In function âmainâ:
    prog4.c:37: warning: passing argument 1 of âinsertâ from incompatible pointer type
    prog4.c:37: warning: passing argument 2 of âinsertâ from incompatible pointer type
    prog4.c:37: warning: passing argument 3 of âinsertâ makes integer from pointer without a cast
    prog4.c:64: error: request for member ânextâ in something not a structure or union
    prog4.c:75: warning: passing argument 1 of âremnodeâ from incompatible pointer type


    Code:
    header.h
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node {
    
            char string[21];
            int num;
            struct node *next;
    
    };
    
    void insert(struct node *, struct node *, int, char *);
    
    void remnode(struct node *);

    Code:
     
    
    #include "header.h"
    
    int main(int argc, char *argv[])
    {
    
            FILE *infile, *outfile;         /*Creates an infile, and
                                            outfile*/
    
            struct node *head;
    
            struct node *current;           /*Creates a head and current
                                            node*/
    
            head = current = NULL;          /*Set the head and current node
                                            to NULL*/
    
            infile = fopen("prog4.in","r"); /*opens infile for reading*/
    
            outfile = fopen("prog4.out","w");/*opens outfile for writing*/
    
            int num;                        /*variable to hold the integer
                                            value read in by fscanf */
    
            char buffer[21];                /*array to hold the characters
                                            read in by fscanf */
    
            while(fscanf(infile, "%d %s", &num, buffer) == 2){
    
                                            /*while the fscanf function retur$
                                            which occurs when it reads in and
                                            integer and the string, do the
                                            following */
    
               insert(&head, &current, &num, buffer);
    
                          struct node *printnode = head;
    
                                            /*create a temporary node for
                                            printing to a file*/
    
                    while(printnode != NULL){
    
                                            /*while printnode exists, or
                                            while another node exists in the
                                            list, do the following*/
    
                            fprintf(outfile, "%s %d\n",
                            (*printnode).string, (*printnode).num);
    
                                            /*print to the outfile the
                                            number and the string from the
                                            current print node*/
    
                            fprintf(outfile, "\n");
    
                            printnode=printnode.next;
    
                                            /*set printnode to be the next no$
    
                    }/*while(printnode != NULL){...*/
    
            } /*while(fscanf(infile, "%d %s", &num, buffer) == 2){...*/
    
            while(head != NULL){
    
    
                    remnode(&head);
    
            }/*while(head != NULL){...*/
    
            fclose(infile);
            fclose(outfile);
    
                 return 0;
    
    } /*end main*/
    Code:
    
    /*insert.c*/
    
    void insert(struct node **front, struct node **current, int num,
    char *buffer[]){
    
            struct node *new = (struct node*)malloc(sizeof(struct node));
    
                            /*creates a node and allocates memory - 1030*/
    
            if(new != NULL) /*checks to makes sure memory is allocated - 1*/
            {
    
                    (*new).string = buffer;
                    (*new).num = num;
    
                    if(*front == NULL)      /*if the front node does not
                                            exist set it equal to new - 1*/
                    {
    
                            *front = new;
                            (*new).next = NULL;
    
                    } /*end if*/
    
    
                    if (strcmp(new.string, *current.string) == 0){
    
                                            /*if new.string is equal to
                                            current.string insert new after
                                            current*/
    
                            (*new).next = NULL;
                            (**front).next = new;
    
                    }/*if (strcmp(new.string, *current.string) == 0){*/
    
                    if (strcmp(new.string, *front.string) < 0){
    
      int i;
    
                    if (strcmp(new.string, *front.string) > 0){
    
                                            /*if new.string comes after
                                            front.string check the next node
                                            by calling insert and passing
                                            front.next into front*/
    
                            insert(&front.next, &current, &num, buffer);
    
    
    
                    }/*if (strcmp(new.string, *current.string) > 0){*/
    
    
    
    
                    *current = new;         /*set the current node to new
                                            - 2*/
    
    
            } /*if(new != NULL){...*/
    
    } /*end insert*/
    Code:
    /*remnode.c*/
    void remnode(struct node **front)
    
                            /*function to remove a node*/
    {
    
            if(front != NULL) /*checks to make sure list is not empty*/
            {
    
                    struct node *temp;      /*create a temporary node*/
    
                    temp = NULL;            /*set the temporary node to
                                            null*/
    
                    temp = *front;          /*set the temp to the front
                                            node*/
    
                    *front = *front.next;
                                            /*set the front node to the
                                            next node*/
    
                    free((*temp).string);   /*free the memory for the string
                                            from the previous front node*/
    
                    free((*temp).num);      /*free the memory for the num
                                            from the previous front node*/
    
                    free(temp);             /*free the memory for previous
                                            front node*/
    
            } /*end if*/
    
    } /*end remnode*/
    Last edited by bolivartech; 10-14-2009 at 09:15 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are passing the address of a pointer, and your function wants a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > header.h
    > void insert(struct node *, struct node *, int, char *);


    >void insert(struct node **front, struct node **current, int num,
    >char *buffer[]){

    To quote Early: Does that seem right to you?

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    I completely understand what you are saying. I guess I didn't quite get my function prototypes right. I had to correct those errors before in my original program, and I figured it out on my own then, I'm not sure why the prototypes and the header file are throwing me off. Below is the error I get after the changes. I'm not sure I've ever seen errors like that, how do I know what line the error is on? Thanks!

    /tmp/ccko1umN.o: In function `main':
    prog4.c.text+0x66): undefined reference to `insert'
    prog4.c.text+0xda): undefined reference to `remnode'
    collect2: ld returned 1 exit status

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Looks like you left off the .o that has the insert and remnode in it. Those are Linker errors, not compiler errors.

    >collect2: ld returned 1 exit status

    LD is your dynamic linker.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    Thanks for the assistance, I included the header file into my other .c files and compiled it as gcc *.c This let me clear up the rest of my errors. I have a segmentation fault now, but I think I can browse through my code and find where I made a mistake. But if anyone sees something above that might lead to that let me know. Thanks again!

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    An easy way to debug is to put this on (almost) every other line:

    printf("%s:%i\n", __FUNCTION__, __LINE__);

    This works for gcc, but I don't know about other compilers.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    Basically put in a print to see where the program get hung up at?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bolivartech
    Basically put in a print to see where the program get hung up at?
    Yes, but you might want to consider using a debugger instead, or at least make those print statements conditional based on some flag variable (or macro).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    Well the printf to check my program has worked out well. I was moving right along finding my errors when it through this at me after another compile. I need to make some changes to my input file to make sure all of my insert function works but since this is an error about free(), at least I think. Then it is dealing with my remnode function. What is it trying to tell me?

    Code:
    in main after insert
    *** glibc detected *** ./a.out: free(): invalid pointer: 0x0000000000601538 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x2b4a2c4ffb23]
    /lib/libc.so.6(cfree+0x8c)[0x2b4a2c50326c]
    ./a.out[0x400ae2]
    ./a.out[0x400a5e]
    /lib/libc.so.6(__libc_start_main+0xf4)[0x2b4a2c4ad8e4]
    ./a.out[0x400729]
    ======= Memory map: ========
    00400000-00401000 r-xp 00000000 00:16 134221046                          /nas/lkemp/CIS3333/prog4/a.out
    00600000-00601000 rw-p 00000000 00:16 134221046                          /nas/lkemp/CIS3333/prog4/a.out
    00601000-00622000 rw-p 00601000 00:00 0                                  [heap]
    2b4a2c273000-2b4a2c28f000 r-xp 00000000 08:01 32787                      /lib/ld-2.5.so
    2b4a2c28f000-2b4a2c2a3000 rw-p 2b4a2c28f000 00:00 0
    2b4a2c48e000-2b4a2c490000 rw-p 0001b000 08:01 32787                      /lib/ld-2.5.so
    2b4a2c490000-2b4a2c5d7000 r-xp 00000000 08:01 32805                      /lib/libc-2.5.so
    2b4a2c5d7000-2b4a2c7d7000 ---p 00147000 08:01 32805                      /lib/libc-2.5.so
    2b4a2c7d7000-2b4a2c7da000 r--p 00147000 08:01 32805                      /lib/libc-2.5.so
    2b4a2c7da000-2b4a2c7dc000 rw-p 0014a000 08:01 32805                      /lib/libc-2.5.so
    2b4a2c7dc000-2b4a2c7e2000 rw-p 2b4a2c7dc000 00:00 0
    2b4a2c7e2000-2b4a2c7ef000 r-xp 00000000 08:01 32830                      /lib/libgcc_s.so.1
    2b4a2c7ef000-2b4a2c9ef000 ---p 0000d000 08:01 32830                      /lib/libgcc_s.so.1
    2b4a2c9ef000-2b4a2c9f0000 rw-p 0000d000 08:01 32830                      /lib/libgcc_s.so.1
    2b4a30000000-2b4a30021000 rw-p 2b4a30000000 00:00 0
    2b4a30021000-2b4a34000000 ---p 2b4a30021000 00:00 0
    7fff7e821000-7fff7e837000 rw-p 7fff7e821000 00:00 0                      [stack]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vdso]
    Aborted (core dumped)

    Code:
    *remnode.c*/
    
    #include "prog4.h"
    
    void remnode(struct node **front)
    
                            /*function to remove a node*/
    {
    
            if(front != NULL) /*checks to make sure list is not empty*/
            {
    
                    struct node *temp;      /*create a temporary node*/
    
                    temp = NULL;            /*set the temporary node to
                                            null*/
    
                    temp = *front;          /*set the temp to the front
                                            node*/
    
                    *front = (**front).next;
                                            /*set the front node to the
                                            next node*/
    
                    free((*temp).string);   /*free the memory for the string
                                            from the previous front node*/
    
                    free(&(*temp).num);             /*free the memory for the num
                                            from the previous front node*/
    
                    free(temp);             /*free the memory for previous
                                            front node*/
    
            } /*end if*/
    
    } /*end remnode*/

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    By the way something I did to get it to compile was change the free(*temp).num; line to include the &. with out it I get:

    remnode.c: In function âremnodeâ:
    remnode.c:28: warning: passing argument 1 of âfreeâ makes pointer from integer without a cast

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    free(&(*temp).num);
    Did num come from malloc? (Looking at this, I doubt it, but you never know.) If you didn't malloc it, don't free it.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Doesn't make any sense to free() each member of struct node when you're malloc()'ing memory for the struct as a whole, as in
    Code:
    /* these two free() calls don't make sense */
    free((*temp).string);
    free(&(*temp).num);
    
    /* just free() struct node which comes a couple of code statements later */
    free(temp);
    Last edited by itCbitC; 10-15-2009 at 01:36 PM.

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by laserlight View Post
    Yes, but you might want to consider using a debugger instead, or at least make those print statements conditional based on some flag variable (or macro).
    One cannot always use a debugger, but almost always can one use print statements (the exception to this, of course, would be bit code for PICs etc).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 01:14 PM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 08:10 AM