# Include
# Include
// To use malloc (), the void main () {char count, * ptr1, * p; ptr1 = malloc (27 * sizeof (char) file must be included )); ptr1 [26] = 0; // Add 0if (ptr1 = NULL) to the string {puts ("there is not enough space to be allocated! \ N ") ;}p = ptr1; // assign the ptr1 pointer address to the pointer pfor (count = 65; count <91; count ++) {* (p ++) = count;} printf ("Print ptr1: \ n"); puts (ptr1); // print all the values in ptr1 printf ("Print P: \ n "); p = ptr1; // The value must be assigned because the P pointer address has already reached 0. Puts (p); free (ptr1); // release the memory allocated by malloc () system ("pause ");}
Running result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;"> # include # Include // To use malloc (), the void main () {char * message of the library file must be included; // declare a pointer named message, but it does not point to any address at this time. Char * message1 = "I am mini, I am very well! "; // Mesage1 is a pointer to this string. // Equivalent to char * message2 [] = {"I am mini, I am very well! "}; // This method can be used if you know how much space to allocate when writing a program. /* You can also use malloc () to dynamically allocate memory space. malloc () is a memory allocation function in C language. Malloc () returns an address. The return type of this function is the void pointer. */Char * str; str = (char *) malloc (100); // allocate 100 bytes of space to the string if (str = NULL) // if there is not enough memory space to allocate, return NULL {printf ("there is not enough space! \ N "); exit (1);} int * numbers; numbers = (int *) malloc (50 * sizeof (int )); // allocate space float * numbers1 for int type; numbers1 = (float *) malloc (10 * sizeof (float); // store one character's memory space char * ptr; ptr = malloc (1); // allocate 1 byte space * ptr = 'X'; // Save the character x to * ptr printf ("the allocated space is complete! \ N "); system (" pause ");}
Running result: