Created
February 21, 2020 05:22
-
-
Save maxkhrichtchatyi/433e63ac57a4f404942c281f44ca403e to your computer and use it in GitHub Desktop.
C Lang access to a variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "point.h" | |
| #include <stdio.h> | |
| int main() { | |
| struct Point* p1 = createPoint(7); | |
| int point = getPoint(p1); | |
| // Everything is OK! | |
| printf("%d", point); | |
| // Something like private, isn't it? | |
| printf("%d", p1->x); | |
| // Or try to use this one. | |
| // p1->x = 10; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "point.h" | |
| #include <stdlib.h> | |
| struct Point { | |
| int x; | |
| }; | |
| struct Point* createPoint(int x) { | |
| struct Point* point = malloc(sizeof(struct Point)); | |
| point->x = x; | |
| return point; | |
| } | |
| double getPoint(struct Point* p1) { | |
| int x = p1->x; | |
| return x; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Point; | |
| struct Point* createPoint(int x); | |
| double getPoint(struct Point *p1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment