0

So I am a Java and C# person recently doing some stuff C. I have a header file that would have a function void update(struct process* foo, float measurements) and in the implementation of the header file (the .c file) I will have the function:

void update(struct process* p,float measurements)
{
  *p.speed = *p.speed + measurements;
  *p.time = *p.time + 1;
  *p.noise = *p.noise + ((measurements)/100);
}

Now in Java I would have to import the class process and it would be all good. However in the .c implementation how would I do that without declaring the struct in the .c file (which would be pointless since I want to pass parameter from another module using it)?

I am quite new in C and may be it's a very basic question but I did an hour search in internet ended up not finding what I am looking for. Maybe my keywords were just poorly chosen.

as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
  • Maybe something's not clear, but you normally put all your declarations in your header files. – Mysticial Sep 14 '12 at 01:13
  • `#include "process.h"` `process` is it there? – nullpotent Sep 14 '12 at 01:13
  • In your implementation file (.c) add at the top add: #include "yourheaderfile.h" (replace yourheaderfile with the name of the file) – Borgleader Sep 14 '12 at 01:14
  • 1
    You probably also want `(*p).` or `p->` instead of `*p.`, since the `.` operator has higher precedence than the the `*` operator. – AusCBloke Sep 14 '12 at 01:15
  • Thanks! So I need to declare another header file to declare the structure? I was thinking about that but then I was afraid of circular reference. – as3rdaccount Sep 14 '12 at 01:27
  • @AusCBloke Thanks! I am a newbie in C so need to catch up a lot. – as3rdaccount Sep 14 '12 at 01:28
  • @ArunavDev I think you should read a C book before proceeding, here is a [list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for you. – RedX Oct 02 '13 at 12:29

1 Answers1

1

You include the file where process structure definition is.

As @AusCBloke noticed, you'll either use (*p). to dereference struct pointer and access its member, or p-> which is syntactic sugar for (*p).

nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • Just like Java I guess I would need to create another class then which would be a header file. I was trying to avoid writing another header file just for a new structure. – as3rdaccount Sep 14 '12 at 01:30