Structure of C language (2)

Source: Internet
Author: User

In the previous understanding of the struct in C I introduced the basic knowledge of the structure, the following code to review:

1 #include <stdio.h> 2 #define LEN 3  4 struct student{            //definition struct 5     char Name[len]; 6     Char Address[le N]; 7     int age; 8}; 9 int main (int argc, char* argv[]) One {ten     struct Student s = {        //Initialize         "Bluemsun", "Nenu", 2514     };    The     struct Student * p;            Defines a pointer to a struct     p = &s;                        Assign the pointer a value of     printf ("s.name =%s,s.address =%s,s.age =%d\n", s.name,s.address,s.age);     printf ("P->name =%s,p->address =%s,p->age =%d\n ", p->name,p->address,p->age);    21}

This is a relatively simple example of the program, in the struct student we define two char arrays, now to consider such a problem. We can use the pointer instead of the array as an argument when we need to use the array, so can we replace the definition of struct with the following code in the program above?

1 struct new_student{            //define struct 2     char * name;3     char * address;4     int age;5};

The answer is yes, but there may be some trouble. Consider the following section of code:

1 struct new_student S1 = {"Zhouxy", "efgh", (+);    2 struct Student s2 = {"Bluemsun", "ABCD", 26};    

This piece of code is correct. But think about where the strings are stored? For struct student variable s2, the string is stored inside the structure, which allocates a total of 40 bytes to store two strings. for new_student variable S1, however, strings are stored anywhere the compiler stores string constants. Only two addresses are stored in the new_student structure. So if you need a struct to hold a string, use a character array member. Is it true that the allocation of memory cannot be done by using pointers? The answer is in the negative. here I explain the function malloc () in C, a function that can dynamically allocate memory at run time. So if we can use it together, it will be our idea. Consider this piece of code:

1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4  5 struct new_student{            // Define struct 6     char * name; 7     char * address; 8     int age 9};10 one int main (int argc, char* argv[])     R[] = "Zhouxy";     new_student S1;     s1.name = (char*) malloc (strlen (str) +1) for the memory allocated for storing the name, and//to     copy the name into the allocated memory.     strcpy (S1.name, STR);     printf ("S1.name =%s\n", s1.name); 21}

The above code is correct, we use the malloc () function to allocate storage space, and then copy the string to the newly allocated space. The understanding here is that thename string is not stored in the structure, but is saved in memory managed by the malloc () function. The structure is simply the address that holds the name string. Another thing to remember is that we use the malloc () function to call the free () function after allocating memory, which may cause a "memory leak."

Structure of C language (2)

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.