Deep pointers and strings in C Language

Source: Internet
Author: User


# 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:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.