Question on function syntax and calling function

This is a discussion on Question on function syntax and calling function within the C Programming forums, part of the General Programming Boards category; Ok, I was given a function to fill out for a programming assignment. The function header is as follows: Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Question on function syntax and calling function

    Ok, I was given a function to fill out for a programming assignment.

    The function header is as follows:

    Code:
    insert_ready(pcb)
    PCB* pcb;
    {
    
    }
    I have never seen syntax like this before, what does it mean?

    Normally all the functions I have written/seen are like this.
    Code:
    insert_ready(PCB * pcb){
    
    }
    The reason I am asking is when I call the function insert_ready in another function I am getting an error.

    line 164: identifier redeclared: insert_ready
    current : function() returning void
    previous: function() returning int : "cpu.c", line 133

    When I am calling the function i am calling it as:
    Code:
    PCB* preempted; // was declared in the calling function
    
    insert_ready(preempted);
    Any ideas?

    Thank you.

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Code:
    insert_ready(pcb)
    PCB* pcb;
    {
    
    }
    That's the old C style function definition. You should avoid it if possible because it's not good at checking types and when mixed with the new style that you (and everyone else ) is used to, you can easily have problems.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    is the suffix the returning value or something?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >is the suffix the returning value or something?
    No, it's the parameter. These two functions are basically the same (except the first is old and ugly and nasty and evil and deprecated ):
    Code:
    funkshun(); /* Declaration */
    
    funkshun(arg) /* Definition */
    int arg;
    {
      /* Do stuff with arg */
    }
    Code:
    int funkshun(int arg); /* Declaration */
    
    int funkshun(int arg) /* Definition */
    {
      /* Do stuff with arg */
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    thanks, I changed it to the new style and it gives me pretty much the same error:

    line 164: identifier redeclared: insert_ready
    current : function(pointer to struct pcb_node {int pcb_id, int size, int creation_time, int last_dispatch, int last_cpuburst, int accu...
    previous: function() returning int : "cpu.c", line 133

    Line 164 is the function insert_ready(PCB* pcb)

    Line 133 is the function call in a function called dispatch.
    The only way to access the pcb that I am trying to call is by a pointer on a structure which contains a pointer to a pcb.

    PTBR->pcb,
    I have tried making the call several ways and it gives an error every time.
    Code:
    PTBR* preempted;
    ....
    preempted=PTBR->pcb;
    insert_ready(preempted);
    
    or
    PTBR *preempted;
    PTBR preempted_pcb;
    
    preempted_pcb=*(PTBR->pcb);
    preempted=&preempted_pcb;
    insert_ready(preempted);

    Any suggestions?

    Thanks

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> previous: function() returning int : "cpu.c", line 133
    You need a function prototype. This error would suggest you simply called your function with no parameters too.

    Stick this in your .h file, or at least at the top of your .c file if you're only using that function in one .c file.

    void insert_ready(PCB*);

    I don't know what return type you require, so change the "void" part to whatever you need.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    hmmm,

    Well I see what you are saying but I cannot do this. The assignment is/has already been written, the main .c file is not able to be modified. We were handed a cpu.c file and were told to write the code for the three functions hence why I did not understand the syntax on the one function.

    I am under the assumption that the main program it is linked to already has the function prototype done for us.

    The way the shell was written it had the function insert_ready() after dispatch.

    Dispatch calls insert_ready(), so i switched their position in the cpu.c file and it compiles fine now.

    Thanks for the heads up.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Swapping functions around to get the to work means you haven't got correct prototyping. It's up to you if you want fix it or not, but I'd suggest you do.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    Well I try to prototype the 3 functions in the .c file we are supposed to modify and it gives me all of these errors:
    line 10: warning: old-style declaration or incorrect type for: cpu_init
    "cpu.c", line 11: syntax error before or at: *
    "cpu.c", line 11: warning: old-style declaration or incorrect type for: insert_ready
    "cpu.c", line 12: warning: old-style declaration or incorrect type for: dispatch
    "cpu.c", line 112: identifier redeclared: cpu_init
    current : function() returning void
    previous: function() returning int : "cpu.c", line 10
    "cpu.c", line 122: identifier redeclared: insert_ready
    current : function() returning void
    previous: function() returning int : "cpu.c", line 11
    "cpu.c", line 205: identifier redeclared: dispatch
    current : function() returning void
    previous: function() returning int : "cpu.c", line 12

    This cpu.c file is used by an Operating System simulator called osp. It is hard to explain, but this simulator program is what calls these functions. Anyways the whole shell of the cpu.c file is provided but with empty functions. If you would like to see what I am talking about here is the same assignment from another schools website.....

    http://data.uta.edu/~ramesh/cse3320/project1-2.html

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    My guess is that you put the prototypes before the definition of PCB, which is typedef'd struct pcb_node.

    The compiler cannot look forward in your source to find things it doesn't recognise, so you have to make sure everything you're using on the current line is already known about.

    There might be other probs too, it's hard to tell without seeing your code. If you want post it here and someone will advise.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    Thank you very much, that seems to work as well. I assumed they had prototyped the 3 functions already but when I got errors I tried putting it in. For some reason at the top of the file, above the constants it would give me an error.

    I prototyped the functions after the typedef's and it worked just fine.

    Thanks for all your help Hammer.

    The rest of the code works fine now. I just have to implement aging on processes preempted back to the ready queue and I am good to go. That is the only warning the simulator issues, and that is because I havent written the code to implement aging yet. I wanted to get a FCFS implementation working first before I add the aging. the Round robin interrupt quantum works as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling function & called function
    By Sajal in forum C Programming
    Replies: 4
    Last Post: 06-05-2009, 09:37 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21