Skip to content

Instantly share code, notes, and snippets.

@maxkhrichtchatyi
Created February 21, 2020 05:22
Show Gist options
  • Save maxkhrichtchatyi/433e63ac57a4f404942c281f44ca403e to your computer and use it in GitHub Desktop.
Save maxkhrichtchatyi/433e63ac57a4f404942c281f44ca403e to your computer and use it in GitHub Desktop.
C Lang access to a variable
#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;
}
#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;
}
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