0

I'm learning C at the moment and have just covered structs. It seems like structs can only have members but no methods restricted to a given struct. Suppose I want to write an add method for a linked list.

In Java, I'd make a linked list class and write a method within that class:

public void add(Node toAdd) {...}

In C, I can write a function that takes in a linked list or a pointer to a linked list and modifies it by adding a node. But is there any way to do this more similarly to how Java does it?

I'm worried I'm going to have a massive file of functions with little organization otherwise...or is this just a pain of C and something that Obj-C improves upon?

Jongware
  • 22,200
  • 8
  • 54
  • 100
anon_swe
  • 8,791
  • 24
  • 85
  • 145

2 Answers2

2

Yes there is a way to do it in C. The specific answer that you are looking for is called function pointer.

You may declare a function pointer in the struct which links to a function that you have defined somewhere else in the file.

The following stackoverflow discussion is relevant to this. here

Here is another relevant discussion that points out that C struct does not support functions like what X3lif has suggested. And in the discussion, the solution is function pointer.

C - function inside struct

Community
  • 1
  • 1
Mox
  • 2,355
  • 2
  • 26
  • 42
  • 1
    This is a good answer, but to make it more OO, you can declare a function as `static` in C, which will prevent its name being emitted by the compiler. This means that it is effectively 'private' to the module (C source file) in which it is declared. The function pointer in the `struct` can still refer to the `static` function, and you will not be able to call it other than through the struct. Win! – Evil Dog Pie May 18 '15 at 16:57
  • To conserve memory and run-time, An indirect approach using a class descriptor would be better: For each strcut you provide this struct (const), which includes all function pointer. Then you just need to store a pointer to this in every instance (object). This is actually what OOP-language compilers create. It also allows to run-time check the type of struct by comparing the class-desc pointer. However, there is one point an OO-language would be easier to use. Note that C++ started as a preprocessor generating C-code. – too honest for this site May 18 '15 at 20:13
2

You are probably confusing some of the concepts here. You can not compare Java with C, but you can compare Java with C++.

  • C is meant for low level programming and does not support object oriented programming.
  • C++ is an extension of C with object oriented features.

So what you are searching for does not exist in C, but in C++. In C++ you can declare classes:

class Foo {
   int member;
   void addToMember(int x) {
       member += x;
   }
};

In C, there are no classes. Usually you define a struct and create a set of functions operating on this struct:

struct Foo {
    int member;
}

void fooInit(Foo *foo);
void fooAddToMember(Foo *foo, int x);

This is actually an old way, how some kind of object oriented programming was done with languages which didn't supported objects.

Today, nobody would do this anymore. One would simple use C++ for object oriented programming. There is actually no need to stick with C. Even most micro controllers today support C++ or other object oriented languages and even modern operating system kernels are moving away from pure C.

Flovdis
  • 2,945
  • 26
  • 49