Skip to content

Instantly share code, notes, and snippets.

@kateolenya
Last active March 14, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateolenya/821a4b3d1daa01d39bd49792ffc035e0 to your computer and use it in GitHub Desktop.
Save kateolenya/821a4b3d1daa01d39bd49792ffc035e0 to your computer and use it in GitHub Desktop.
Bypassing encapsulation with pointers in C
#include "private_var.h"
#include <stdio.h>
int main()
{
struct Contact * Tony;
Tony = create_contact();
int * mobile_number_is_here = (int *)Tony;
printf("Mobile number: %d\n", *mobile_number_is_here);
int * home_number_is_here = mobile_number_is_here + 1;
*home_number_is_here = 1;
printf("Modified home number: %d\n", *home_number_is_here);
delete_contact( Tony );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment