Thread: Unofficial extensions to C, structs with functions, does that exist?

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Unofficial extensions to C, structs with functions, does that exist?

    I know, if I want OOP I should go to cpp, but I still prefer C for no real reason. Is there a C compiler that supports defining structure members that are functions? GCC by default, doesn't. I didn't try any other standards.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Turbo C - no functions allowed in a struct.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I've never used such a compiler. You can have function pointers in your struct, though, which is a pretty good approximation--you just have to remember to point them somewhere first.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    From Google: Using function pointers,
    Code:
    #include <stdio.h>
    
    
    struct t {
            int a;
            void (*fun) (int * a);
    } ;
    
    void get_a (int * a) {
            printf (" input : ");
            scanf ("%d", a);
    }
    
    int main () {
            struct t test;
            test.a = 0;
    
            printf ("a (before): %d\n", test.a);
            test.fun = get_a;
            test.fun(&test.a);
            printf ("a (after ): %d\n", test.a);
    
            return 0;
    }
    It even validates the params you pass. Is there any way to set the initial value of t.fun to get_a?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Create an initialization function that creates a t and returns it.

    Quote Originally Posted by User Name: View Post
    I know, if I want OOP I should go to cpp, but I still prefer C for no real reason. Is there a C compiler that supports defining structure members that are functions? GCC by default, doesn't. I didn't try any other standards.
    Obviously not. OOP is not the point of C. You're not going to see it anytime soon.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by User Name:
    Is there any way to set the initial value of t.fun to get_a?
    There's the normal way of initialising a struct object, e.g.,
    Code:
    struct t test = {0, get_a};
    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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Code:
    struct t test = {0, get_a};
    o_O
    Because for some reason that little "feature" is still in the C standard, I tend to downplay it. It's evil. Don't use it. So to speak.
    Another way to do it is (and IMHO, the proper way):
    Code:
    struct t test = {0, &get_a};
    Nevertheless, I would say it is preferable to do it in a separate function. An initializing function. The more function pointers you put, the easier it is to simply call the function instead of duplicating code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In case if you are interested, there's GObject.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You don't need & in front of function name. It's optional.
    Using C99,
    Code:
    struct t test = { .fun = get_a };  // If I'm not wrong,other members init to proper zero according to C99
    Last edited by Bayint Naung; 07-21-2010 at 05:05 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it's optional, and no, I don't recommend doing it because it's wrong. Some old compatibility crap.
    To take the address of something, you use the address-of operator. No exceptions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Because for some reason that little "feature" is still in the C standard, I tend to downplay it. It's evil. Don't use it. So to speak.
    Another way to do it is (and IMHO, the proper way):
    Likewise, when you want to call a function through a function pointer f, remember to write (*f)(args), not f(args). The latter is evil. Don't use it. So to speak.

    EDIT:
    Quote Originally Posted by Elysia
    To take the address of something, you use the address-of operator. No exceptions.
    Likewise, when you want to pass the address of the first element of an array x in order to simulate passing the array by reference, do not write foo(x). Rather, always write foo(&x[0]). Remember boys and girls, no exceptions. Allowing an array to be converted to a pointer to its first element is evil.
    Last edited by laserlight; 07-21-2010 at 05:09 AM.
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. you have a point.
    (*f)(args) vs f(args): is one a compatibility syntax or is it simply two different syntax ways of doing things (kind of like (*p).me vs p->me)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    100% agree with laserlight.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, purism is evil mmkay?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Extensions or backwards compatibility constructs are evil O_o And multiple syntaxes for the same thing is evil, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  2. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  3. passing structs to functions?
    By Neildadon in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 04:31 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. probs with structs and functions
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-08-2002, 11:52 PM

Tags for this Thread