结构体中指针

结构体中带有指针的情况

#include<stdio.h>struct man
{char *name;int age;
};int main()
{struct man m = {"tom",20};printf("name = %s, age = %d\n",m.name,m.age);return 0;
}

运行结果:

exbot@ubuntu:~/wangqinghe/C/20190714$ gcc struct.c -o struct

exbot@ubuntu:~/wangqinghe/C/20190714$ ./struct

name = tom, age = 20

 

如果修改m.name的值

#include<stdio.h>
#include<string.h>struct man
{char *name;int age;
};int main()
{struct man m = {"tom",20};strcpy(m.name,"mike");printf("name = %s, age = %d\n",m.name,m.age);return 0;
}

exbot@ubuntu:~/wangqinghe/C/20190714$ gcc struct.c -o struct

exbot@ubuntu:~/wangqinghe/C/20190714$ ./struct

段错误 (核心已转储)

 

会出现以上错误。

 

将指针改位数组:

#include<stdio.h>
#include<string.h>struct man
{char name[256];int age;
};int main()
{struct man m = {"tom",20};strcpy(m.name,"mike");printf("name = %s, age = %d\n",m.name,m.age);return 0;
}

编译运行:

exbot@ubuntu:~/wangqinghe/C/20190714$ gcc struct.c -o struct

exbot@ubuntu:~/wangqinghe/C/20190714$ ./struct

name = mike, age = 20

 

分析:

常量内存中的数值是不能修改的。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>struct man
{char *name;int age;
};int main()
{//struct man m = {"tom",20};struct man m;m.name = malloc(sizeof(char) * 100);m.age = 20;strcpy(m.name,"mike");printf("name = %s, age = %d\n",m.name,m.age);return 0;
}

编译运行:

exbot@ubuntu:~/wangqinghe/C/20190714$ gcc struct.c -o struct

exbot@ubuntu:~/wangqinghe/C/20190714$ ./struct

name = mike, age = 20

#include<stdio.h>
#include<string.h>
#include<stdlib.h>struct man
{char *name;int age;
};int main()
{struct man *p = malloc(sizeof(struct man));p->name = malloc(sizeof(char) * 100);strcpy(p->name,"tom");p->age = 30;printf("name = %s, age = %d\n",p->name,p->age);free(p->name);free(p);return 0;
}

指针在内存的存储方式:

exbot@ubuntu:~/wangqinghe/C/20190714$ gcc struct.c -o struct

exbot@ubuntu:~/wangqinghe/C/20190714$ ./struct

name = tom, age = 30

 

END

转载于:https://www.cnblogs.com/wanghao-boke/p/11183110.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384986.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python使用opencv提取视频中的每一帧、最后一帧,并存储成图片

提取视频每一帧存储图片 最近在搞视频检测问题&#xff0c;在用到将视频分帧保存为图片时&#xff0c;图片可以保存&#xff0c;但是会出现(-215:Assertion failed) !_img.empty() in function cv::imwrite问题而不能正常运行&#xff0c;在检查代码、检查路径等措施均无果后&…

结构体参数

结构体作为函数参数&#xff1a; #include<stdio.h> #include<stdlib.h> #include<string.h>struct student {char name[10];int age; };void print_student(struct student s) {printf("name %s,age %d\n",s.name,s.age); } void set_studen…

线程间通信之eventfd

线程间通信之eventfd man手册中的解释&#xff1a; eventfd()创建了一个“eventfd对象”&#xff0c; 通过它能够实现用户态程序间(我觉得这里主要指线程而非进程)的等待/通知机制&#xff0c;以及内核态向用户态通知的机制&#xff08;未考证&#xff09;。 此对象包含了一个…

【linux 开发】定时器使用setitimer

setitimer Linux 为每一个进程提供了 3 个 setitimer 间隔计时器&#xff1a; ITIMER_REAL&#xff1a;减少实际时间&#xff0c;到期的时候发出 SIGALRM 信号。ITIMER_VIRTUAL&#xff1a;减少有效时间 (进程执行的时间)&#xff0c;产生 SIGVTALRM 信号。ITIMER_PROF&#…

文件操作(写)

/*** file.c ***/ #include<stdio.h>int main() {//用写的方式打开一个文件 //w的意思是文件如果不存在&#xff0c;就建立一个文件&#xff0c;如果文件存在就覆盖FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file1.txt","w");fputs(&qu…

定时器timerfd

1.为什么要加入此定时器接口 linux2.6.25版本新增了timerfd这个供用户程序使用的定时接口&#xff0c;这个接口基于文件描述符&#xff0c;当超时事件发生时&#xff0c;该文件描述符就变为可读。我首次接触这个新特性是在muduo网络库的定时器里看到的&#xff0c;那么新增一个…

文件操作(读)

读一行&#xff1a; #include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn 10; int main() {char s[1024] {0};FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r");//第一个参数是一个内存地址&…

timerfd与epoll

linux timerfd系列函数总结 网上关于timerfd的文章很多&#xff0c;在这儿归纳总结一下方便以后使用&#xff0c;顺便贴出一个timerfd配合epoll使用的简单例子 一、timerfd系列函数 timerfd是Linux为用户程序提供的一个定时器接口。这个接口基于文件描述符&#xff0c;通过文…

文件操作(解密加密)

文件加密&#xff1a; #include<stdio.h> #include<string.h> #include<stdlib.h>void code(char *s) {while(*s){(*s);s;} }int main() {char s[1024] {0};FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r");FILE *p…

linux僵尸进程产生的原因以及如何避免产生僵尸进程defunct

给进程设置僵尸状态的目的是维护子进程的信息&#xff0c;以便父进程在以后某个时间获取。这些信息包括子进程的进程ID、终止状态以及资源利用信息(CPU时间&#xff0c;内存使用量等等)。如果一个进程终止&#xff0c;而该进程有子进程处于僵尸状态&#xff0c;那么它的所有僵尸…

linux下僵尸进程(Defunct进程)的产生与避免

在测试基于 DirectFBGstreamer 的视频联播系统的一个 Demo 的时候&#xff0c;其中大量使用 system 调用的语句&#xff0c;例如在 menu 代码中的 system("./play") &#xff0c;而且多次执行&#xff0c;这种情况下&#xff0c;在 ps -ef 列表中出现了大量的 defunc…

文件操作函数

fopen()函数参数&#xff1a; r 只读的方式打开文件。 打开成功返回文件指针&#xff0c; 打开失败返回NULL r 以读写方式打开文件。 文件必须存在 rb 以二进制模式读写文件&#xff0c;文件必须存在 rw 读写一个二进制文件&#xff0c;允许读和写 w 打开只写文件&…

读过的最好的epoll讲解

首先我们来定义流的概念&#xff0c;一个流可以是文件&#xff0c;socket&#xff0c;pipe等等可以进行I/O操作的内核对象。 不管是文件&#xff0c;还是套接字&#xff0c;还是管道&#xff0c;我们都可以把他们看作流。 之后我们来讨论I/O的操作&#xff0c;通过read&#xf…

文件操作函数(读写)

文件文本排序&#xff1a; 数组冒泡&#xff1a; #include<stdio.h>void swap(int *a,int *b) {int temp *a;*a *b;*b temp; }void bubble(int *p,int n) {int i;int j;for(i 0; i < n; i){for(j 1; j < n - i; j){if(p[j - 1] > p[j]){swap(&p[j-1],&…

文件操作(升级)

计算字符串“25 32 ” #include<stdio.h> #include<string.h>int calc_string(char *s) {char buf1[100] {0};char oper 0;char buf2[100] {0};int len strlen(s);int i;for(i 0; i < len; i){if( s[i] || - s[i] || * s[i] || / s[i] ){strncpy…

C语言指针转换为intptr_t类型

C语言指针转换为intptr_t类型 1、前言 今天在看代码时&#xff0c;发现将之一个指针赋值给一个intptr_t类型的变量。由于之前没有见过intptr_t这样数据类型&#xff0c;凭感觉认为intptr_t是int类型的指针。感觉很奇怪&#xff0c;为何要将一个指针这样做呢&#xff1f;如是果…

nginx epoll详解

nginx epoll 事件模型 nginx做为一个异步高效的事件驱动型web服务器&#xff0c;在linux平台中当系统支持epoll时nginx默认采用epoll来高效的处理事件。nginx中使用ngx_event_t结构来表示一个事件&#xff0c;先介绍下ngx_event_t结构体中成员的含义&#xff1a; struct ngx_ev…

Inotify机制

描述 Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件&#xff0c;也可以检测整个目录。当检测的对象是一个目录的时候&#xff0c;目录本身和目录里的内容都会成为检测的对象。 此种机制的出现的目的是当内核空间发生某种事件之后&#xff0c;可以立即通…

文件操作(二进制文件加密解密)

加密 #include<stdio.h> #include<string.h>void code(char *p,size_t n) {size_t i;for(i 0; i < n; i){p[i] 3;} }int main() {FILE *p1 fopen("./a.txt","r");FILE *p2 fopen("./b.txt","w");char buf[1024] {…

北京加密机现场select问题

问题描述 北京项目通过调用我们提供的库libsigxt.a与加密机通信&#xff0c;c/s架构&#xff0c;客户端启用多个线程&#xff0c;每个线程流程有以下三步&#xff0c;连接加密机&#xff0c;签名&#xff0c;关闭链接。在正常运行一段时间后会出现不能连接加密机服务问题。 连…