c语言结构体 this指针吗,c语言结构体内嵌结构体指针_C语言中的结构指针

c语言结构体内嵌结构体指针

Prerequisite:

先决条件:

Structures in C programming language. C编程语言中的结构。 Dynamic Memory allocation functions in C. C中的动态内存分配功能。

Example:  In this tutorial, we will use a structure for “student”, structure members will be "name" (string type), "age" (integer type), "roll number" (integer type).

示例:在本教程中,我们将为“学生”使用结构,结构成员将为“名称”(字符串类型),“年龄”(整数类型),“卷数”(整数类型)。

结构声明 (Structure declaration)

struct student{

char name[50];

int age;

int rollno;

};

Here,

这里,

struct is the keyword to define a structure. struct是定义结构的关键字。 student is the name of the structure. 学生是结构的名称。 name is the first structure member to store age with maximum of 50 characters, age is second structure member to store age of the student, and rollno is the third structure member to store rol number of the student. name是存储年龄最多的50个字符的第一个结构成员, age是存储学生年龄的第二个结构成员, rollno是存储学生的编号的第三个结构成员。

指向结构声明的指针 (Pointer to structure declaration)

struct student *ptr;

将内存分配给结构指针 (Allocating memory to the pointer to structure)

ptr = (struct student*)malloc(sizeof(struct student));

The statement will declare memory for one student’s record at run time. Read more about the dynamic memory allocation in C programming language.

该语句将在运行时声明对一个学生的记录的记忆。 阅读有关C编程语言中动态内存分配的更多信息。

使用指针访问结构成员 (Accessing structure members using pointer)

The arrow operator (->) is used to access the members of the structure using pointer to structure. For example, we want to access member name, and ptr is the pointer to structure. The statement to access name will be ptr->name.

箭头运算符 ( -> )用于使用指向结构的指针访问结构的成员。 例如,我们要访问成员名称 ,而ptr是指向structure的指针 。 访问名称的语句将是ptr-> name 。

指向结构的C程序 (C program for pointer to structure)

#include

//structure definition

struct student{

char name[50];

int age;

int rollno;

};

//main function

int main(){

//pointer to structure declaration

struct student *ptr;

//allocating memory at run time

ptr = (struct student*)malloc(sizeof(struct student));

//check memory availability

if( ptr == NULL){

printf("Unable to allocate memory!!!\n");

exit(0);

}

//reading values of the structure

printf("Enter student details...\n");

printf("Name? ");

scanf("%[^\n]", ptr->name); //reads string with spaces

printf("Age? ");

scanf("%d", &ptr->age);

printf("Roll number? ");

scanf("%d", &ptr->rollno);

//printing the details

printf("\nEntered details are...\n");

printf("Name: %s\n", ptr->name);

printf("Age: %d\n", ptr->age);

printf("Roll number: %d\n", ptr->rollno);

//freeing dynamically allocated memory

free(ptr);

return 0;

}

Output

输出量

Enter student details...

Name? Amit shukla

Age? 21

Roll number? 100

Entered details are...

Name: Amit shukla

Age: 21

Roll number: 100

翻译自: https://www.includehelp.com/c/pointer-to-structure.aspx

c语言结构体内嵌结构体指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值