Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created November 3, 2018 18:50
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 haxpor/70d5d1b42df32971556823748ab2c786 to your computer and use it in GitHub Desktop.
Save haxpor/70d5d1b42df32971556823748ab2c786 to your computer and use it in GitHub Desktop.
Test careful point in using pointer and dereferencing it to value type in C. Merit point is to use pointer and be careful when working with deref value variable.
#include <stdio.h>
#include <stdlib.h>
struct myStr
{
int valuea;
int valueb;
};
typedef struct myStr myStr;
int main (int argc, char* args[])
{
myStr* a = malloc(sizeof(myStr));
myStr v = *a;
v.valuea = 10;
v.valueb = 20;
printf("pointer a\n");
printf("valuea: %d\n", a->valuea);
printf("valueb: %d\n", a->valueb);
printf("v\n");
printf("valuea: %d\n", v.valuea);
printf("valueb: %d\n", v.valueb);
free(a);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment