其中哪一个是更高效和更好的代码?还是有其他方法我应该这样做?
typedef struct foo {
int width;
int height;
} foo;
...下面两个示例中的 typedef,但它实际上是一个任意结构...
foo *new_foo (int width, int height) {
foo *f
if ((f = malloc(sizeof(foo)))==NULL) return NULL;
f->width = width;
f->height = height;
return foo;
}
void del_foo (foo *f) {free(f);}
int main () {
int width = 3;
int height = 4; // arbitrary values
foo *f
f = new_foo(width, height)
// do something with foo here
del_foo(f);
}
或者
int new_foo (foo *f, int width, int height) {
f->width = width;
f->height = height;
return 0;
}
int main () {
int width = 3;
int height = 4; // arbitrary values
foo *f
if ((f = malloc(sizeof(foo)))==NULL) return NULL;
new_foo(f, width, height)
// do something with foo here
free(f);
}
谢谢!对于任何错别字,我深表歉意。