C-language structure passing values--passing values by pointers

Source: Internet
Author: User

There are three kinds of methods for the transfer of structures, which pass the structure , pass the pointer , and pass the structure's own parameters . The way to pass pointers is the most significant difference between the two methods of passing is actually the address of the struct, in the process of passing the value, the pointer needs to initialize the allocated memory (that is, the malloc () function allocates space to the pointer)

Take a look at the following code:

There are two points to note:

(1) When the method setting type is a struct book type, but also a pointer type, can be said (struct book && pointer type)

(2) When declaring a struct book &&pointer type at 32, 33 lines of code, be sure to do a memory allocation for the pointer type once

1 /*2 * This instance program is used to show how structure parameters are passed in the method body3 the method of passing the parameter is to pass the parameter in the form of a pointer4 the GetInfo () method is used to assign a struct pointer to a value operation5 the Showinfo () method is used to output the structure body6 */ 7#include <stdio.h>8#include <stdlib.h>9 #defineMax_size 2Ten #defineMax_title_size 30 One #defineMax_author_size 30 A //constructs a book type of struct body -  /* - *title is a char type the Author Char type - Price Float Type -  */  - struct Book + { -     CharTitle[max_title_size]; +     CharAuthor[max_author_size]; A     floatPrice ; at      - };  - /* - declaring the GetInfo () Showinfo () method - */ - structBook * GetInfo (structBook *Lib); in structBook * Showinfo (structBook *Lib); - intMain () to { +     structBook *Lib; -Lib= (structBook *)malloc(sizeof(structBook )); thelib=GetInfo (Lib); * Showinfo (Lib); $     return 0; Panax Notoginseng } - structBook * GetInfo (structBook *Lib) the { +printf"Please enter the title: \ t"); AGets (lib->title); theprintf"Please enter the author name: \ t"); +Gets (lib->author); -printf"Please enter the price of the book: \ t"); $scanf"%f",& (lib->Price) ); $     returnLib; - } - structBook * Showinfo (structBook *Lib) the { -printf"The title is%s \ T and the author is%s \ t and the price is%f",WuyiLib->title,lib->author,lib->Price ); the}

The result of the operation is:

C-language structure passing values--passing values by pointers

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.