Results 1 to 10 of 10

Thread: Newbie C programming

  1. #1
    Join Date
    Feb 2011
    Beans
    3

    Newbie C programming

    Hi I wanted to see, for no real reason other than to do it, if I could emulate object orientated methods with a C struct that has a function. Maybe my non-working code can work to explain what I mean :)
    Code:
    #include <stdio.h>
    
    struct person{
            char name[10];
            void (*setname)();
    }
    
    int main(){
            struct person test;
            test.setname=&setname;
            *(test.setname)(&test,"John");
            printf("name: %s",test.name);
            return 0;
    }
    void setname(struct person *self, char name[]){
            self->name=name;
    }
    http://pastebin.com/0v1ziYnc


    This yields:
    Code:
    gcc -o struc oo.c
    oo.c:8: error: two or more data types in declaration specifiers
    oo.c: In function ‚main‚:
    oo.c:10: error: ‚setname‚ undeclared (first use in this function)
    oo.c:10: error: (Each undeclared identifier is reported only once
    oo.c:10: error: for each function it appears in.)
    oo.c:11: error: void value not ignored as it ought to be
    oo.c:13: error: incompatible types in return
    oo.c:8: warning: return type of ‚main‚ is not ‚int‚
    oo.c: In function ‚setname‚:
    oo.c:16: error: incompatible types in assignment
    I don't know what the funny characters are about maybe it's a putty thing?
    Last edited by AlexanderPike; February 19th, 2011 at 01:59 AM. Reason: added more info

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Newbie C programming

    there are a few issues:
    - a semicolon is missing after the closing bracket of the struct

    - a forward declaration for setname is missing as it is defined below main
    place this or the whole function before main:
    Code:
    void setname(struct person *self, char *name);
    note I changed char name[]. I find this makes it clearer, see next point

    - character arrays decay into pointers on stack changes, this includes function pointers. So char name[] is exactly same as char * name in a function declaration.
    This makes clear that setname does not work. you have to do this:
    Code:
    #include <string.h>
    void setname(struct person *self, char * name){
      strncpy(self->name,name,sizeof(self->name));
      // or safer version
      strncpy(self->name,name,sizeof(self->name) - 1);
      self->name[sizeof(self->name) - 1] = 0;
    }
    - the function pointer calling syntax is wrong. You are calling the function and dereferencing the result (which is void).
    either this:
    Code:
    (*test.setname)(&test,"John")
    //or
    (test.setname)(&test,"John")
    or a bit clearer:
    Code:
    (*(test.setname))(&test,"John")
    Last edited by MadCow108; February 19th, 2011 at 02:23 AM.

  3. #3
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Newbie C programming

    In this line(9), you do not need to specify struct again:
    Code:
    struct person test;
    Should be:
    Code:
    person test;
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  4. #4
    Join Date
    Feb 2011
    Beans
    3

    Re: Newbie C programming

    Thanks MadCow108! It works perfectly now. Here's the completed code if anyone is interested.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct person{
            char name[10];
            void (*setname)();
    };
    void setname(struct person *self, char *name);
    
    int main(){
            struct person test;
            test.setname = &setname;
            (*test.setname)(&test,"John");
            printf("name: %s",test.name);
            return 0;
    }
    void setname(struct person *self, char * name){
      strncpy(self->name,name,sizeof(self->name));
    }

  5. #5
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Newbie C programming

    Quote Originally Posted by cgroza View Post
    In this line(9), you do not need to specify struct again:
    Code:
    struct person test;
    Should be:
    Code:
    person test;

    in C you do need the struct. This is different in C++

    you can only skip it if you do a typedef:
    Code:
    typedef struct mystruct {
    ...
    } mystruct;
    // or 
    struct mystruct_s {
    ...
    };
    typedef mystruct_s mystruct_t;

  6. #6
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Newbie C programming

    Quote Originally Posted by MadCow108 View Post
    in C you do need the struct. This is different in C++

    you can only skip it if you do a typedef:
    typedef struct mystruct {
    ...
    } mystruct;
    // or
    struct mystruct_s {
    ...
    };
    typedef mystruct_s mystruct_t;
    I know that in C++ that is not necessary, and people say that they try to maintain compatibility.
    Anyway, sorry for misleading information.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  7. #7
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Newbie C programming

    Quote Originally Posted by AlexanderPike View Post
    Thanks MadCow108! It works perfectly now. Here's the completed code if anyone is interested.
    ...
    Yes that is an excellent bit of research into how Object orientedness might be done

    Just incase you want to know more accurately what a C++ compiler would be generating... well it would be closer to this:
    PHP Code:
    // gcc Cvtable.c ... this is straight C code for an OOP paradigm
    #include <stdio.h>
    #include <string.h>

    // these just to make it more intelligible
    typedef void (*fptr)();
    typedef char * (*cptr)();

    // all instances of person class must have a vtable pointer
    struct person{
        
    fptr *vtable;
        
    char name[10];
    };

    // the methods of person class are setname and getname
    void setname(struct person *selfchar name){
      
    strncpy(self->name,name,sizeof(self->name));
    }

    char getname(struct person *self){
        return 
    self->name;
    }

    // this is person class vtable
    fptr person_vtable[] = {
        (
    fptr) &setname,
        (
    fptr) &getname
    };

    int main(){
        
    // construct a person class instance
        
    struct person test;
        
    // which implies setting it's vtable pointer
        
    test.vtable person_vtable;

        
    // call the setname method on this instance "test" of class person
        
    (*test.vtable[0])(&test,"John");

        
    // call the getname method on that instance
        
    return !printf("name: %s\n", ((cptr) *test.vtable[1])(&test));

    so now you also know what a vtable does cuz it means just setting ONE pointer in the object for ALL the virtual methods of said class
    Last edited by worksofcraft; February 19th, 2011 at 03:13 AM. Reason: tidied it up and added comments
    Take nothing but photographs. Leave nothing but footprints.

  8. #8
    Join Date
    Feb 2011
    Beans
    3

    Re: Newbie C programming

    That is pretty interesting. Would you say that was idomatic? I can imagine with alot of methods that works much better.

    I just found this PDF which looks pretty interesting http://www.planetpdf.com/codecuts/pdfs/ooc.pdf discussing oo using ansi c, but it looks to be a pretty hefty read.

  9. #9

    Re: Newbie C programming

    Idiomatic C doesn't try to emulate C++.

  10. #10
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Newbie C programming

    Quote Originally Posted by AlexanderPike View Post
    That is pretty interesting. Would you say that was idomatic? I can imagine with alot of methods that works much better.

    I just found this PDF which looks pretty interesting http://www.planetpdf.com/codecuts/pdfs/ooc.pdf discussing oo using ansi c, but it looks to be a pretty hefty read.
    Soz IDK what idomatic means, but I do know that originally C++ was just done as preprocessor for standard C and then you could see it was producing that kind of thing for the compiler to compile.
    Take nothing but photographs. Leave nothing but footprints.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •