hi everyone,
i'm trying learn plain old c; i'm reasonably proficient c# new c. i'm trying figure out whether there way "calculated member" (for want of better name) in struct. example, starting following:
code:
typedef struct { int width; int height; } rectangle;
i'd able add sort of "int area = width * height;". it'll more complicated that, sort of thing possible in c? i've created function called rectangle_area takes rectangle , returns int, i'm not sure whether there's way "link" main struct. don't yet know terms should searching can please newbie out?
do mean add function struct? can function pointer, it's bit convoluted since can not refer struct within struct. benefit of oop languages c++ or obj-c both have "this" pointer that. in case of c, you'd need add struct argument function.
edit: example
code:
#include <stdio.h> typedef struct rectangle{ int width; int height; int (*area)(struct rectangle *rect); } rectangle; int area(rectangle *rect) { return rect->width * rect->height; } int main(void) { rectangle rect = {2, 3, area}; printf("area: %d\n", rect.area(&rect)); return 0; }
i need type out struct name in struct declaration or use void pointer or area function declaration don't know rectangle since appear row below.
Forums Macs Mac Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
Comments
Post a Comment