#include #include #include struct Student { int id; float gpa; char* name; }; void print_std (struct Student std){ printf("Name: %s\n", std.name); printf("ID: %d\n", std.id); printf("GPA: %f\n", std.gpa); return; } int main(int argc, char const *argv[]) { //I/O int var = 20; printf("the value of var is: %d\n", var); printf("Enter a new value of var: "); scanf("%d", &var); //don't forget the '&' printf("now the new value of var is: %d\n", var); //structure struct Student s; s.id = 123456; s.gpa = 4.0; s.name = "Some Student"; print_std(s); //pointers int * p1 = &var; int** p2 = &p1; //int printf("content of var: %d\n", var); printf("deference of p1: %d\n", *p1); printf("double deference of p2: %d\n", **p2); //int* printf("addr of var: %p\n", &var); printf("content of p1: %p\n", p1); printf("deference of p2: %p\n", *p2); //int ** printf("addr of p1: %p\n", &p1); printf("content of p2: %p\n", p2); //int *** printf("addr of p2: %p\n", &p2); //void * void* v_ptr = &var; //printf("deference v_ptr w/o casting: %d\n", *v_ptr);// error!! printf("deference v_ptr w/ casting: %d\n", *((int*)v_ptr)); v_ptr = &s; print_std(*((struct Student*)v_ptr)); struct Student *s_ptr = v_ptr; print_std(*s_ptr); int *array = malloc(100 * sizeof(int)); free(array); array = NULL; //c-strings char* name = "Roger"; //constant c-string //name[0] = 'o'; //seg fault const char* name2 = "some name"; //name2[0] = 'j'; //compiling error //char* str = malloc(sizeof(char) * 64); char str[64] = "hello"; // for static array, the &str, &str[0], str --> same addr // for dynamic array, the &str[0], str --> same addr, but &str is different // cannot get the size of a dynamic array using sizeof() //char* str = malloc(sizeof(char) * 64); //str = "hello"; printf("size of str: %d\n", sizeof(str)); printf("strlen of str: %d\n", strlen(str)); //str = "hello world"; //printf("strlen of str: %d\n", strlen(str)); return 0; }