自己学习总结,学会总结分享
分类: 嵌入式
2016-03-26 01:37:47
很容易得出p[-2]=10,p[-1]=20,p[0]=30,p[1]=40,p[2]=50,p[3]=60;
p++,指针p往后移一个单位,则p=a+3,此时p[0]=*(p+0)=40;
*p++, 是先取出*p的值,再执行p++操作, 则*p++=40,p=a+4;
*++p,p先往后移一个单位,再取对应地址的值,则*++p=60;
++*p, 则是先取出*p的值,对*p的值再加1,则++*p=61;
int a=0; int * const p=&a; //指向整型的常指针 指针指向的内容可变,指针的指向不可变(地址不可变) const int *p=&a; //指向常整型的指针 指针指向可变,指向内容不可变 const int * const *p=&a;//指向常整型的常指针 指针指向内容和指向都不可变
#include
#include
int main()
{
int a=100, b=200;
const int *p = &a;
int * const q = &b;
printf("a=%d, *p=%d\n", a, *p);
printf("b=%d, *q=%d\n", b, *q);
/* 不可利用*p 间接更改a变量值
*p=b;
*/
/* 但可以将p指针指向另一变量的地址 */
p=&b;
printf("*p=%d\n", *p);
/*===========================*/
/* 不可更改 q 指针指向另一变量的地址
q=&a;
*/
/* 但可以利用q指针间接更改b变量值 */
*q=888;
printf("*q=%d\n", *q);
getch();
return 0;
}
1.
int arr[]= {100, 101, 102};
int *ptr = arr;
size = (sizeof arr/ sizeof (arr[0]) );
&arr[0] == arr+0 ==ptr+0 ==100的地址
*(arr+0) ==arr[0] ==ptr[0] == *(ptr+0) ==100
2.
int k[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
&k[1][2] == *(k + 1) + 2 =k[1]+2 ==6的地址
上面的表达式加*号为数值6
*(k[1] + 2) == *( (k[1]) +2 ) == k[1][2] ==6
3
char *parr[] = {"Department", "of", "Information", "Management"};
*parr[3] == **(parr+3) ==M
*(parr+2) == Information
4.
int arr2[3][4] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22};
int *parr[3] = {arr2[0], arr2[1], arr2[2]};
*(parr + 1) == parr[1] + 0 ==8的地址
*(parr[1] + 1) ==10
结构体
1. 内存地址
/* 声明结构变量 */
struct employee
{
char id[7]; /* ID号码 */
char name[20]; /* 员工姓名 */
int salary; /* 所得薪资 */
};
/* 定义结构变量,并设定其初始值 */
struct employee manager = {"D54321", "Peter", 35000};
/* 定义结构指针变量ptr,将它指向结构变量manager的地址 */
struct employee *ptr = &manager;
/* 输出内存的地址 */
&ptr= 18ff1c
ptr= 18ff20
&manager= 18ff20
manager.id = 18ff20
ptr->id = D54321
(*ptr).id =D54321
2.结构体定义
struct student {
char firstname[20];
char *lastname;
int score;
};
struct student st1, *st2;
scanf("%s", st1.firstname); /* 不可以使用指定运算符 */
st1.lastname="小英"; /* 不可以使用scanf函数来输入 */
st2 = (struct student *)malloc(sizeof(struct student));
scanf("%s", st2->firstname); /* 不可以使用指定运算符 */
st2->lastname="小明"; /* 不可以使用scanf函数来输入 */
3. 结构体传递地址
#include
#include
struct student {
char name[20];
int score;
char *passdown;
};
void passOrdown(struct student *);
void output(struct student *);
int main()
{
struct student stu;
printf("请输入姓名: ");
scanf("%s", stu.name);
printf("请输入C语言的分数: ");
scanf("%d", &stu.score);
passOrdown(&stu);
output(&stu);
getch();
return 0;
}
void passOrdown(struct student *p)
{
if (p->score >= 60)
p->passdown = "PASS"; //调用时使用&传递地址,导致函数中改变变量的值可以返回,在output函数中使用得以输出
else
p->passdown = "DOWN";
}
void output(struct student *q)
{
int i;
printf("\n\n%10s %10s %20s\n", "Name", "Score", "Pass or Down");
for(i=1; i<=42; i++)
printf("=");
printf("\n");
printf("%10s %10d %20s\n", q->name, q->score, q->passdown);
}
4.结构体数组
struct student stu[3];
int i;
for(i=0; i<3; i++) {
printf("请输入第 #%d 位同学的姓名: ", i+1);
scanf("%s", stu[i].name);
printf("请输入第 #%d 位同学C语言的分数: ", i+1);
scanf("%d", &stu[i].score);
printf("\n");
}
passOrdown(stu);
output(stu);
getch();
return 0;
}
void passOrdown(struct student *p)
{
int i;
for(i=0; i<3; i++) {
if (p->score >= 60)
p->passdown = "PASS";
else
p->passdown = "DOWN";
p++;
}
}
例子:
struct student st[]= { {"John", 90, st+1},
{"Mary", 85, st+2},
{"Peter", 92, st} };
void output(struct student *q)
{
int i;
printf("\n\n%10s %10s %20s\n", "Name", "Score", "Pass or Down");
for(i=1; i<=42; i++)
printf("=");
printf("\n");
for(i=0; i<3; i++) {
printf("%10s %10d %20s\n", q->name, q->score, q->passdown);
q++;
}
}
#include
#include
#include
int main()
{
struct node {
char *name;
int score;
struct node *next;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
p->name = "Mary";
p->score = 80;
p->next = NULL;
q = (struct node *) malloc(sizeof(struct node));
q->name = "John";
q->score = 90;
q->next = p;
printf("%s %d\n", q->name, q->score);
printf("%s %d\n", q->next->name, q->next->score);
getch();
return 0;}
struct student {
char *name;
int score;
struct student *next;
};
struct student st[]= { {"John", 90, st+1},
{"Mary", 85, st+2},
{"Peter", 92, st} };
struct student *ptr[]={st, st+1, st+2};
++(*ptr)->next)->name =peter
(*ptr)->next->next->name)=peter
双链表
点击(此处)折叠或打开
- /* doublyListRandom.c */
- /* 双链表的加入与删除: 以分数的高低加以排序 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- void init_f(void); /* 初始化链表,建立一空节点为HEAD */
- void insert_f(void); /* 插入函数 */
- void delete_f(void); /* 删除函数 */
- void display_f(void); /* 输出函数 */
- void modify_f(void); /* 修改函数 */
- struct student {
- char name[20]; /* 姓名 */
- int score; /* 分数 */
- struct student *llink; /* 节点左链结 */
- struct student *rlink; /* 节点右链结 */
- };
- struct student *ptr, *head, *tail, *current_n, *prev;
- int main()
- {
- char option1;
- init_f();
- while(1){
- printf("\n ****************\n");
- printf(" 1.insert\n");
- printf(" 2.delete\n");
- printf(" 3.display\n");
- printf(" 4.modify\n");
- printf(" 5.quit\n");
- printf(" *******************\n");
- printf(" Enter your choice (1-5): ");
- option1 = getche();
- switch(option1){
- case '1':
- insert_f();
- break;
- case '2':
- delete_f();
- break;
- case '3':
- display_f();
- break;
- case '4':
- modify_f();
- break;
- case '5':
- printf("\n");
- return 0;
- }
- }
- }
- void init_f(void) /* 设一HEAD,将左右链结都指向本身 */
- {
- ptr = (struct student *) malloc(sizeof(struct student));
- strcpy(ptr->name, "0");
- ptr->llink = ptr;
- ptr->rlink = ptr;
- head = ptr;
- tail = ptr;
- }
- /* 根据分数的高低加入 */
- void insert_f(void)
- {
- char s_temp[4];
- ptr = (struct student *) malloc(sizeof(struct student));
- printf("\n Student name : ");
- gets(ptr->name);
- printf(" Student score: ");
- gets(s_temp);
- ptr->score = atoi(s_temp);
- prev = head;
- current_n = head->rlink;
- while((current_n != head) && (current_n->score >= ptr->score)) {
- prev = current_n;
- current_n = current_n->rlink;
- }
- ptr->rlink = current_n;
- ptr->llink = prev;
- prev->rlink = ptr;
- current_n->llink = ptr;
- }
- void delete_f(void)
- {
- char del_name[20], ans;
- int count = 0;
- if(head->rlink == head)
- printf("\n No student record\n");
- else {
- printf("\n Delete student name: ");
- gets(del_name);
- prev = head;
- current_n = head->rlink;
- while ((current_n != head) && (strcmp(current_n->name,
- del_name)!=0)) {
- prev = current_n;
- current_n = current_n->rlink;
- }
- if (current_n != head) {
- /* 确认是否要删除 */
- printf(" Are you sure? ");
- ans=getche();
- if (ans == 'Y' || ans == 'y') {
- prev->rlink = current_n->rlink;
- current_n->rlink->llink = prev;
- free(current_n);
- printf("\n Student %s has been deleted\n",del_name);
- }
- }
- else
- printf("\n Student %s not found\n",del_name);
- }
- }
- void modify_f(void)
- {
- int count = 0;
- char n_temp[20], s_temp[4];
- if(head->rlink == head)
- printf("\n No student recond\n"); /* 无数据显示错误 */
- else {
- printf("\n Modify student name: ");
- gets(n_temp);
- current_n=head->rlink;
- while ((current_n != head) && (strcmp(current_n->name , n_temp)!=0)){
- prev = current_n;
- current_n = current_n->rlink;
- }
- if (current_n != head) {
- printf("\n **************************\n");
- printf(" Student name : %s\n",current_n->name);
- printf(" Student score: %d\n",current_n->score);
- printf(" **************************\n");
- printf(" Please enter new score: ");
- gets(s_temp);
- current_n->score = atoi(s_temp);
- printf("\n %s student record(s) modified\n", n_temp);
- }
- else // 找不到数据则显示错误
- printf("\n Student %s not found\n",n_temp);
- }
- }
- void display_f(void)
- {
- int count = 0;
- if(head->rlink == head)
- printf("\n No student record\n");
- else {
- printf("\n\n NAME SCORE\n");
- printf(" --------------------\n");
- current_n = head->rlink;
- while(current_n != head) {
- printf(" %-10s %3d\n", current_n->name,
- current_n->score);
- count++;
- current_n = current_n->rlink;
- if(count % 20 == 0) getch(); /* 每次输出20条数据 */
- }
- printf(" ---------------------\n");
- printf(" Total %d record(s) found\n", count);
- }
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开