目录:

  1. array
  2. '\0' 与 EOF
  3. 二维字符数组(字符串数组)

1. array:

参考:C 数组

参考:C 字符串

参考:C笔记之NULL和字符串结束符'\0'和EOF

总结:【个人理解,可能不全面】数组对于数字型和字符型有不同,数字数组就是实际长度,而字符型数组需要增加一个 '\0',所以对于数字数组可以这样定义  int num[] = {, , , , }; ,而字符型数组需要预先定义长度,否则没有 '\0' 会出错,需要这样定义 char str[6] = {'h', 'e', 'l', 'l', 'o'};或者 char str[] = "hello",会自动添加 '\0'。

#include <stdio.h>
#include <string.h> int main(void)
{
char *r = "hello";
char s[10] = {'h','e','l','l','o'};
char ss[] = {'h','e','l','l','o'};
char t[] = "hello"; int num[] = {1, 2, 3, 4, 5}; printf ("r = %s\ns = %s\nss = %s\nt = %s\n", r, s, ss, t);
printf ("The length of r is %ld.\n", sizeof(r)); // address
printf ("The strlen of r is %ld.\n", strlen(r)); // actual length
printf ("The length of s is %ld.\n", sizeof(s)); // s[10] with '\0'
printf ("The length of ss is %ld.\n", sizeof(ss)); // print with error
printf ("The length of t is %ld.\n", sizeof(t)); // t[6] with '\0'
printf ("The strlen of t is %ld.\n", strlen(t)); // actual length
printf ("The length of num is %ld.\n", sizeof(num)/sizeof(num[0])); // actual length for (int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
printf("%d\n", num[i]);
} /* r = hello*/
/* s = hello*/
/* ss = hellohello ----error----*/
/* t = hello*/
/* The length of r is 8.*/
/* The strlen of r is 5.*/
/* The length of s is 10.*/
/* The length of ss is 5. ----without '\0'----*/
/* The length of t is 6.*/
/* The strlen of t is 5.*/
/* The length of num is 5.*/
/* 1*/
/* 2*/
/* 3*/
/* 4*/
/* 5*/ return 0;
}

2. '\0' 与 EOF 的区别

参考:C语言中EOF是什么意思?

前者作为字符串的结尾标志

后者作为文件的结尾标志

如下所示:(0,与 -1)

#include <stdio.h>
#include <stdlib.h> int main(){
printf("\'\\0\' is %d\n", '\0');
printf("EOF is %d\n", EOF); /* '\0' is 0*/
/* EOF is -1*/ return 0;
}

3. 二维字符数组(字符串数组)

多维字符数组可以用双星号来表示

参考:C语言的字符串数组

参考:指向指针的指针

#include <stdio.h>
#include <stdlib.h> int main(){
// str is the pointer of array which is still pointer
char *str[] = {"alex", "bn", "lee"}; // str[0] means the first array (e.g. "alex")
printf("The first string is %s.\n", str[0]);
// str + 1 means the pointer of second array
// add * in front of it, then it will become the pointer of chars
// and it can be printed as string
printf("The second string is %s.\n", *(str + 1));
// Just like above one
// *(str + 2) means the pointer of chars (e.g. "lee")
// *(str + 2) + 1 means the pointer of the second character
// *((str + 2) + 1) means element of that pointer
printf("The second character of the third string is %c.\n", *(*(str + 2) + 1)); /* outputs:*/
/* The first string is alex.*/
/* The second string is bn.*/
/* The second character of the third string is e.*/ return 0;
}

双星号:不能直接定义信息,需要指向

#include <stdio.h>
#include <stdlib.h> int main(){
// str is the pointer of array which is still pointer
char *str_o[] = {"alex", "bn", "lee"};
// you can't assignment **str directly since it can only be used for string (* to array)
// but you can define a **str point to str_o
char **str = str_o; // str[0] means the first array (e.g. "alex")
printf("The first string is %s.\n", *str);
// str + 1 means the pointer of second array
// add * in front of it, then it will become the pointer of chars
// and it can be printed as string
printf("The second string is %s.\n", *(str + 1));
// Just like above one
// *(str + 2) means the pointer of chars (e.g. "lee")
// *(str + 2) + 1 means the pointer of the second character
// *((str + 2) + 1) means element of that pointer
printf("The second character of the third string is %c.\n", *(*(str + 2) + 1)); /* outputs:*/
/* The first string is alex.*/
/* The second string is bn.*/
/* The second character of the third string is e.*/ return 0;
}

4. typedef & struct

struct 基本格式如下:(注意最后的 semicolon)

 struct date {
int day;
int month;
int year;
}; // struct date will become one thing
struct date birthday;

typedef 可以将一个整体的东西用一个新的字符串表示

typedef struct {
int a;
int b;
} Pair;
...
Pair x; or typedef struct date Date Date d;

struct 中的指针使用

// pointerstruct.c
#include <stdio.h> typedef struct {
int name;
int salary;
} Worker; // Worker is now a type, like char and int int main(void) {
Worker w, *wp; // define variables with the new type wp = &w;
*wp.salary = 125000; // generates a compiler error (see below)
*(wp.salary) = 125000; // so does this (same as previous line) w.salary = 125000; // is probably what you want
(*wp).salary = 125000; // or you can use the pointer, but this looks kludgy
wp->salary = 125000; // this is the 'sexy' way to use pointers return 0; // being lazy, should be EXIT_SUCCESS
}

简化如下:

structTypeName  s, *sp;
sp = &s; // then the following are equivalent: s.element
(*sp).element
sp->element

5. Makefile

An example of a rule in a Makefile is:

ex1: ex1.c ex1.h
dcc -O3 -o ex1 ex1.c

It is important to note that dcc above is preceded by a tab (and not spaces).

Every file in UNIX has a timestamp, which is the time it was created or last modified.

  • If you type in the command make, then make will compare the timestamps of each of the files in the dependencies with the target.

  • If any of these dependency files is newer than the target, make will execute the command again (which usually makes the target again).

  • If none of the files is newer, make simply reports that the target is up to date.

You can have multiple targets. Below we show three targets:

all: ex1 ex2

ex1: ex1.c ex1.h
dcc -O3 -o ex1 ex1.c ex2: ex2.c ex2.h
dcc -O3 -o ex2 ex2.c

You can then type make ex1 or make ex2 as you wish.

If you type simply make, then the default is to make the first target only, which in this case is all, which in turn has as its dependencies the other two targets. This command will make either or both the executables if they are older than their course code.

You can generalise the Makefile by defining and using variables, such as:

CC=dcc
CFLAGS=-O3 all: ex1 ex2 ex1: ex1.c ex1.h
$(CC) $(CFLAGS) -o ex1 ex1.c ex2: ex2.c ex2.h
$(CC) $(CFLAGS) -o ex2 ex2.c

The variables will be substituted when the Makefile is executed.

You can also add a target to clean up your directory after you have finished:

CC=dcc
CFLAGS=-O3 all: ex1 ex2 ex1: ex1.c ex1.h
$(CC) $(CFLAGS) -o ex1 ex1.c ex2: ex2.c ex2.h
$(CC) $(CFLAGS) -o ex2 ex2.c clean:
rm -f ex1 ex2 ex1.o ex2.o core
  • the clean target has no dependencies, and here removes the executable, the object files, and core dumps

  • the command make clean will execute the rm instruction

  • the -f switch means that rm will not complain if any files are do not existent

6. 链表(Linked List)

  typedef struct node {
int data;
struct node *next;
} LinkedL;

or

  typedef struct node {
int data;
struct node *next;
} *LNode, *LinkedL;

列表遍历,可以通过 for 语句或者 while 语句,判别条件都是最后是否为 NULL

// traverse the list (of 2 elements)
for (LinkedL *p = n1; p != NULL; p = p->next) {
printf("%d\n", p->data);
} // or LinkedL *p = n1;
while (p != NULL) {
printf("%d\n", p->data);
p = p->next;
}

7. struct 内存空间

看里面最大的为基准来考虑,就是如果最大为 1 byte,则此为单元,如果 8 bytes 最大,则以此为单元,不满足的需要 padding。

#include <stdio.h>
#include <stdlib.h> struct somedata1 {
char age; // 1 byte
char weight; // 1 byte
char gender; // 1 byte
}; // aligns to 3 bytes struct somedata2 {
int age; // 4 bytes
char *weight; // 8 bytes
char gender; // 1 byte
}; // aligns to 24 bytes struct somedata3 {
int age; // 4 bytes
int weight; // 4 bytes
char gender; // 1 byte: either 'f' or 'm'
}; // aligns to 12 bytes int main() { printf("somedata1: %ld\n", sizeof(struct somedata1));
printf("somedata2: %ld\n", sizeof(struct somedata2));
printf("somedata3: %ld\n", sizeof(struct somedata3)); /* outputs:*/
/* somedata1: 3*/
/* somedata2: 24*/
/* somedata3: 12*/ return 0;
}

【423】COMP9024 Revision的更多相关文章

  1. 【434】COMP9024 Exercises Revision

    目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...

  2. 【433】COMP9024 复习

    目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...

  3. 【411】COMP9024 Assignment1 问题汇总

    1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...

  4. 【432】COMP9024,Exercise9

    eulerianCycle.c What determines whether a graph is Eulerian or not? Write a C program that reads a g ...

  5. 【403】COMP9024 Exercise

    Week 1 Exercises fiveDigit.c There is a 5-digit number that satisfies 4 * abcde = edcba, that is,whe ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  8. 【原】低版本MyEclipse整合高版本Tomcat

    [使用工具] 1.MyEclipse_6.0.1GA_E3.3.1_FullStackInstaller 2.Tomcat 7.0 [问题描述] 直接在MyEclipse中整合,因为这个版本的MyEc ...

  9. 通用js函数集锦<来源于网络/自己> 【一】

    通用js函数集锦<来源于网络/自己>[一] 1.返回一个全地址2.cookie3.验证用户浏览器是否是微信浏览器4.验证用户浏览器是否是微博内置浏览器5.query string6.验证用 ...

随机推荐

  1. Scala配置环境变量Linux

    1.下载.上传并解压scala-2.11.6.tgz 2.配置环境变量vim /etc/profile 增加如下代码: export SCALA_HOME=/usr/scala/scala-2.11. ...

  2. Android中sp和px之间关系探究

    记得当时在刚接触Android时都在说不要用px,要用sp,所以在实际工作当中当然就按照这个规则,所以都要将px换算成sp,而我在实际工作中的换算规则是dp=px * 1.5,而且用这种规则到现在基本 ...

  3. 【小顶堆的插入构造/遍历】PatL2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制   将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x a ...

  4. springboot的入门

    SpringBoot SpringBoot是SpringMVC的升级版,简化配置,很可能成为下一代的框架 1.新建项目 怎么创建springBoot项目呢? 创建步骤复杂一点点 New Project ...

  5. 关于strlen和sizeof的使用

    在学习C语言中发现strlen和sizeof的关系不是很明确,今天来总结一下这两个的区别: sizeof 是运算符,用来计算字节数,在计算字符串数组大小时包含(\0) 在编译时计算大小,参数可以是数组 ...

  6. 使用bootstrap的栅格布局,用row后出现横向滚动条

    原因: **row默认有:margin-left:-15px; margin-right:-15px: 解决办法: **row外层需要包裹container或者container-fluid,一句话就 ...

  7. [React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks

    When using useQuery from Apollo React Hooks, the request will be sent automatically after the compon ...

  8. Helm:kubernetes应用包管理工具

    概要 Helm:kubernetes应用包管理工具 K8s部署应用的时候,应用会通过yaml描述信息调用K8s-api:Helm即是管理这些Yaml的应用包管理工具 组成 Helm包含5个部分 Hel ...

  9. Mac 升级 Python2.7 到 Python3.5

    1.去 Python 官网下载一个版本的包 https://www.python.org/downloads/mac-osx/ 2.安装之后,去  /Library/Frameworks/Python ...

  10. PHP实现多文件上传的一些简单方法

    下面我们就通过具体的代码示例,为大家介绍PHP实现多文件上传的一些简单方法. 第一种方法:利用单个文件上传方法 一段简单的form表单代码如下: <!DOCTYPE html> <h ...