Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mutator method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C=== In file student.h: <syntaxhighlight lang="C"> #ifndef _STUDENT_H #define _STUDENT_H struct student; /* opaque structure */ typedef struct student student; student *student_new(int age, char *name); void student_delete(student *s); void student_set_age(student *s, int age); int student_get_age(student *s); char *student_get_name(student *s); #endif </syntaxhighlight> In file student.c: <syntaxhighlight lang="C"> #include <stdlib.h> #include <string.h> #include "student.h" struct student { int age; char *name; }; student *student_new(int age, char *name) { student *s = malloc(sizeof(student)); s->name = strdup(name); s->age = age; return s; } void student_delete(student *s) { free(s->name); free(s); } void student_set_age(student *s, int age) { s->age = age; } int student_get_age(student *s) { return s->age; } char *student_get_name(student *s) { return s->name; } </syntaxhighlight> In file main.c: <syntaxhighlight lang="C"> #include <stdio.h> #include "student.h" int main(void) { student *s = student_new(19, "Maurice"); char *name = student_get_name(s); int old_age = student_get_age(s); printf("%s's old age = %i\n", name, old_age); student_set_age(s, 21); int new_age = student_get_age(s); printf("%s's new age = %i\n", name, new_age); student_delete(s); return 0; } </syntaxhighlight> In file Makefile: <syntaxhighlight lang="makefile"> all: out.txt; cat $< out.txt: main; ./$< > $@ main: main.o student.o main.o student.o: student.h clean: ;$(RM) *.o out.txt main </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)