// intro to malloc #include main(int argc, char *argv[]) { int i, n, *ip; unsigned char *cp; if(argc > 1) n = atoi(argv[1]); else n = 1000000; // datatypes have sizes printf("sizeof int is %d\n", sizeof(int)); printf("sizeof char is %d\n", sizeof(char)); printf("sizeof unsigned char is %d\n", sizeof(unsigned char)); // we can dynamically allocate space using malloc(nbytes); ip = malloc(n * sizeof(int)); cp = malloc(n); // we "know" char is always just 1 byte // we can access the space using pointer notation for(i = 0; i < n; i++) { *(ip + i) = i; *(cp + i) = i; } // or, we more commonly use array notation to do the same thing for(i = 0; i < n; i++) { ip[i] = i; cp[i] = i; } printf("ip[50000] is %d\n", ip[50000]); // why is this one different? printf("cp[50000] is %d\n", cp[50000]); }