博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

随笔分类 -  C语言

摘要:一个数组,查找这个数组中最大的一个连续子数组。比如{1, 2, 3, –11},这个数组中最大子数组为{1, 2, 3}。 算法复杂度: O(nlgn) 伪代码: C代码: #include <stdio.h>typedef struct index { int left; int right; int sum;} index_t;index_t find_max_cros... 阅读全文

posted @ 2011-09-02 11:27 天地玄黄 阅读(2323) 评论(0) 推荐(1)

摘要:假设我们定义一个数组char a[16]; 那么a与&a分别代表什么呢?它们完全一样吗? a其实代表的是数组中首元素的地址,即a[0]的地址,&a代表的是整个数组的起始地址。我们做个测试就可以很容易知道。 我们分别测试a a+1 &a &a+1这四个值,它们分别为: a 0x0012fcc8 a+1 0x0012fcc9 &a 0x0012fcc8 &a+1 0x0012fcd8 a和&a的值虽... 阅读全文

posted @ 2011-07-23 10:19 天地玄黄 阅读(1118) 评论(1) 推荐(2)

摘要:对于一维数组,我们可以把它考虑成一长溜格子,每个格子就是一个元素,每个元素是一个数值。 对于多维数组,我们也可以把它看做一长溜的格子,每个格子就是一个元素,只不过每个元素又是一个数组。这样依次深入。 阅读全文

posted @ 2011-07-23 09:51 天地玄黄 阅读(228) 评论(0) 推荐(0)

摘要:一直以为typedef必须在相应的数据类型之后才可定义,原来在前面也可以: #include <stdio.h>#include <stdlib.h>/* the typedef is before the struct */typedef struct pcap_if* pcap_if_p;struct pcap_if { struct pcap_if *next; int a;};int m... 阅读全文

posted @ 2011-07-22 14:45 天地玄黄 阅读(762) 评论(0) 推荐(1)

摘要:用C编写一个程序(软件),首先要对要编写的程序进行分析。以下是我自己总结的一些设计要点: 1、 首先对程序进行分析,把整个程序分成几个不同的部分,每个部分完成一个功能。这样,每个功能就可以用一个.c文件来实现。 2、 每个.c文件对应的.h文件里存放的是这个部分可以被其他部分使用的函数,如果这些函数的变量或者返回值中有我们自己定义的数据类型,那么就要把相应的数据类型也写在头文件中。.h文件就是外... 阅读全文

posted @ 2011-07-21 16:12 天地玄黄 阅读(666) 评论(0) 推荐(1)

摘要:刚开始我也不知道是怎么回事,后来注意到,在C中,凡是结构体定义时前面都要用struct。 比如我定义了一个struct pcap_pkthdr {...}; 那么在使用这个结构体定义变量时就必须写:struct pcap_pkthdr *header; 阅读全文

posted @ 2011-07-18 20:21 天地玄黄 阅读(376) 评论(0) 推荐(0)

摘要:到现在为止,我看过的书籍基本上是一脉相承的,逐渐深入计算机内部: · C语言深度解剖 ----- 这个是电子书,遇到问题还经常看一看 · C专家编程(Exptert C Programming) ----- 这个看过去就忘了,在图书馆借的书 · C traps and pitfalls ---- 这个也是在图书馆借的,翻译的书,基本上也不怎么记得了 · Linux C一站式编程 ----- 这本... 阅读全文

posted @ 2011-05-30 16:21 天地玄黄 阅读(891) 评论(1) 推荐(0)

摘要:在Linux中使用线程相关的东西就要使用到这个头文件,但这还不算。如果仅仅使用这个头文件,会出现错误: undefined reference to `pthread_create'collect2: ld returned 1 exit statusmake: *** [threadid] Error 1 之所以出现这样的错误,是因为我们没有链接相应的函数库。所以在编译的时候要加上 –lpth... 阅读全文

posted @ 2011-05-26 14:55 天地玄黄 阅读(12057) 评论(0) 推荐(1)

摘要:在C语言中,函数名竟然可以和struct类型名相同。看下面的程序。定义了struct foo; 和 void foo(struct foo *)两个函数。 #include <stdio.h>struct foo { int a; int b;};void foo(struct foo *f){ printf("%d, %d", f->a, f->b);}int main(){ struct f... 阅读全文

posted @ 2011-05-16 15:12 天地玄黄 阅读(1605) 评论(0) 推荐(0)

摘要:原文网址:http://www.adp-gmbh.ch/cpp/gcc/create_lib.html 还是看原文吧,不要看下面了,原文中有各种格式。 Here's a summary on how to create a shared and a static library with gcc. The goal is to show the basic steps. I do not want... 阅读全文

posted @ 2011-05-06 16:05 天地玄黄 阅读(400) 评论(0) 推荐(0)

摘要:一个C语言的可执行程序(a.out)通常被分成以下几个部分: · Text segment: 即汇编中的.text segemnt。machine instruction, sharable, only one copy in memory, read-only · Initialized data: 即汇编中的.data segment。是已经初始化的数据。是出现在所有的程序之外的变量(全局变量... 阅读全文

posted @ 2011-05-06 15:28 天地玄黄 阅读(390) 评论(0) 推荐(0)

摘要:一般我们写main函数都是这样的: int main(int argc, char **argv); 一个process调用exec函数给新的将要执行的program传递command-line argument。(When a program is executed, the process that does the “exec” can pass command-line argument... 阅读全文

posted @ 2011-05-06 14:53 天地玄黄 阅读(171) 评论(0) 推荐(0)

摘要:C程序会使用 "start-up routine” 来调用main()函数,开始执行main() 函数。这个 "start-up routine”从kernel中获取参数和环境,然后设定好,然后调用main()函数开始执行。 当main()函数调用return 0; 返回的时候,返回的地方也是"start-up routine”。由start-up routine在执行一些操作(一般是调用exi... 阅读全文

posted @ 2011-05-06 10:44 天地玄黄 阅读(247) 评论(0) 推荐(0)

摘要:副标题: 在汇编中使用Standard C Library。 main函数的执行过程。 main函数与Standard C Library的交互。 C程序是怎么开始和结束的: 在这里,一个C程序就是一个process Note: The only way a program is executed by the kernel is when one of the exec functions is... 阅读全文

posted @ 2011-05-06 10:32 天地玄黄 阅读(765) 评论(0) 推荐(0)

摘要:4.6 Write a utility like cp(1) that copies a file containing holes, without writing the bytes of 0 to the output file. 我自己写了一整程序来完成这个操作。 首先要使用fig3.2的程序生成一个带有hole的文件file.hole。 #include "apue.h"#include... 阅读全文

posted @ 2011-05-03 15:28 天地玄黄 阅读(535) 评论(0) 推荐(0)

摘要:3.6 If you open a file for readwrite with the append flag, can you still read from anywhere in the file using lseek? Can you use lseek to replace existing data in the file? Write a program to verify t... 阅读全文

posted @ 2011-04-29 22:13 天地玄黄 阅读(369) 评论(0) 推荐(0)

摘要:在C语言中,static关键字的引入最先是为了表示 退出一个块后(即退出某个函数之后)仍然存在的局部变量。随后,C中的static 有了第二种含义:用来表示不能被其他文件访问的全局变量和函数。 static 对三个东西进行修饰:局部变量、全局变量、函数。 1、修饰局部变量 static 修饰局部变量时,表示这个变量在这个函数退出之后并不消失,当再次进入这个函数时,这个变量中存放的值还是和退出时候... 阅读全文

posted @ 2011-04-23 14:36 天地玄黄 阅读(333) 评论(0) 推荐(0)

摘要:C语言程序各种各样的编码风格,我比较推崇的一种是K&R 书上的模式。下面是从《Unix Network Programming》上抄的一个样例,可以参看一下书写格式: 1 /* This is a C program templete. 2 */ 3 #include "unp.h" 4 5 int main(int argc, char **argv) 6 { 7 char *ptr, **p... 阅读全文

posted @ 2011-04-22 19:48 天地玄黄 阅读(787) 评论(0) 推荐(0)

摘要:我在本地目录定义了dg_cli()函数,在一个Archieve File中也定义了这个函数。 本地目录中的dgcliconnect.c 中就是这个函数的定义。另一个同样的函数在../libunp.a中。 dgcliconnect.c 编译之后的文件为 dgcliconnect.o。 链接的时候,先链接本地的.o文件,再链接库文件,不会出错: cattz@Ubuntu:~/unp/unpv13e/c... 阅读全文

posted @ 2011-04-21 11:14 天地玄黄 阅读(3112) 评论(0) 推荐(0)

摘要:在这里,我自己写一个自己的Makefile的例子,说明一下Makefile是怎么写的。 准备工作:为了使我们的代码更加接近真实的大型程序,我们可以从网络上下载一些写好的源代码。我所使用的是 “Unix Network Programming” 这本书中的源代码。代码下载地址为:www.unpbook.com,下载的文件为unpv13e.tar.gz。使用命令 tar –zxvf unpv13e.... 阅读全文

posted @ 2011-04-14 22:17 天地玄黄 阅读(2969) 评论(0) 推荐(0)