C语言函数指针
来源:互联网 发布:全国十大淘宝村集群 编辑:程序博客网 时间:2024/06/07 19:02
1.
使用语句实现下面所描述的功能:
(1) 定义一个double类型的指针head,并初始化为NULL。
double *head = NULL;
(2) 定义一个double类型的变量salary,并初始化为8000.75
,让head指向
salary。double salary = 8000.75;
head = &salary;
(3) 使用&显示变量salary所占内存的地址值。
printf("变量salary所占内存的地址值为:%x\n", &salary);
(4) 使用head显示salary所占内存的地址值。
printf("变量salary所占内存的地址值为:%x\n", head);
(5) 分别通过salary本身和head显示变量salary的值。
printf("变量salary为:%lf\n", salary);
printf("变量salary为:%lf\n", head);
(6) 再定义一个double类型的指针tail,并初始化为NULL。
double *tail = NULL;
(7) 通过赋值的方式,让tail也指向salary,要求使用指针
间赋值完成操作。
tail = head;
printf("%lf\n", tail);
(8) 显示指针变量tail的值。
(9) 通过tail显示所指变量的值。
printf("tail=%lf\n", *tail);
(10) 通过tail修改salary的值为3023.25。
*tail = 3023.25;
(11) 通过head显示所指变量的值。
printf("head=%lf\n", *head);
(12) 定义一个int类型的变量age,初始化为28。
int age = 28;
(13) 将age的地址赋值给head,即使head重新指向age,查看
执行结果。
head = &age;
(14) 通过tail将变量salary的值加100。
*tail = *tail + 100;
(15) 使用salary显示变量的值。
printf("salary=%lf\n", salary);
(16) 使用sizeof显示head、tail、salary的大小。
printf("sizeof (int head) = %d\n", sizeof(head));
printf("sizeof (int tail) = %d\n", sizeof(tail));
printf("sizeof (int salary) = %d\n", sizeof(salary));
2.
编写一个程序,程序能使用指针完成输出变量值的功能:定义所
有基本数据类型的变量并初始化变量的值,然后定义所有基本数
据类型的指针并使用指针输出各变量的值;再使用指针改变各变
量的值,使用变量重新输出。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int a = 1;
double b = 5.00;
float c = 9.0;
int *p;
double *p1;
float *p2;
p = &a;
p1 = &b;
p2 = &c;
printf("int变量的值为:%d\n", *p);
printf("double变量的值为:%lf\n", *p1);
printf("float变量的值为:%f\n", *p2);
*p = 5;
*p1= 6.00;
*p2 = 7.0;
printf("指针改变int变量的值为:%d\n", *p);
printf("指针改变double变量的值为:%lf\n", *p1);
printf("指针改变float变量的值为:%f\n", *p2);*/
return EXIT_SUCCESS;
}
3.
定义整型数组长度为10,分别用指针和数组名访问数组中的元素。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int arr[10];
int i;
printf("请输入10个整数:\n");
fflush(stdout);
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
printf("通过数组名访问到的数组元素为\n");
for(i=0;i<10;i++){
printf("%d ",arr[i]);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int arr[10];
int i;
int *p=arr;
printf("请输入10个整数:\n");
fflush(stdout);
for(i=0;i<10;i++){
scanf("%d",p++);
}
p=arr;
printf("通过指针访问到的数组元素为::\n");
for(i=0;i<10;i++,p++){
printf("%d ",*p);
}*/
4.
输入a,b,c三个整数,按先大后小的顺序输出。(20分钟)
1.#include <stdio.h>
#include <stdlib.h>
int main(void){
int a,b,c,x;
int *pa,*pb,*pc;
pa=&a;
pb=&b;
pc=&c;
printf("请输入3个整数\n");
fflush(stdout);
scanf("%d%d%d",pa,pb,pc);
if(*pa>*pb){
x=*pa;*pa=*pb;*pb=x;
}if(*pa>*pc){
x=*pa;*pa=*pc;
*pc=x;
}if(*pb>*pc){
x=*pb;
*pb=*pc;
*pc=x;
}printf("这3个数由小到大的排列顺序为%d,%d,
%d",*pa,*pb,*pc);
return EXIT_SUCCESS;
}
5.
编写一程序,将两个字符串连接起来,s1接在s2的后面。
#include<stdio.h>
#include <stdlib.h>
int main(void){
char str1[100]="Hello";
char str2[100]="World ";
char *s1;
char *s2;
s1=str1;
s2=str2;
strcat(s2,s1);
printf("s1接在s2的后面为:\n%s",s2);
//方法二:不应用strcat函数
/*void func(char *t,char *s){
while(*t){
t++;
}
while(*s){
*t=*s;
t++;
s++;
}
*t='\0';
}
int main(){
char str2[100]="hello ";
char str1[100]="world!";
func(str2,str1);
printf("s1接在s2的后面为:\n%s",str2);
return EXIT_SUCCESS;
}
6.
完成下列任务:
(1)从键盘接受一个字符串。
(2)定义指针变量,通过指针变量访问字符串,并输出。
(3)输出指针变量。
#include <stdio.h>
#include <stdlib.h>
int main(void){
*char str[100];
printf("请输入一个字符串:\n");
fflush(stdout);
scanf("%s",str);
printf("输入的字符串为:\n%s\n",str);
char *p;
p=str;
printf("通过指针变量p访问字符串为:\n%s\n",p);
printf("指针变量为:\n%s\n",str);*/
return EXIT_SUCCESS;
}
7.
完成下列任务:
(1)定义一个字符型变量
(2)定义一个void型的指针变量
(3)通过指针变量访问字符型变量
(4)输出指针变量的值,输出指针变量。
#include <stdio.h>
#include <stdlib.h>
Int main(void){
char str[100]={"hello world!"};
void *p;
p=str;
printf("指针变量p的值为:\n%s\n",(char*)p);
return EXIT_SUCCESS;
}
8.
完成下列任务:
(1)定义整形变量a,
(2)定义指针变量p,
(3)定义一个指针的指针p1,用于指向指针p,
(4)分别输出a,p,p1,
(5)分别输出*p,*p1。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i=5;
int *p;
int **p1;
p=&i;
p1=&*p;
printf("i=%x\np=%x\np1=%x\n",i,p,p1);
printf("*p=%d\n*p1=%d\n",*p,*p1);
return EXIT_SUCCESS;
}
9.
完成下列任务:
(1)定义整型数组,数组长度10;定义指针变量并指向整型数组
,
(2)求指针变量的字节,并输出
(3)指针变量加2后再输出指针变量的值和地址,
(4)定义两个指针p和p1,分别指向数组中第一个元素和第五个
元素,两指针做减法运算,输出运算结果。
(5)指针p和p1进行比较,并输出比较后的值。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int *b = a;
printf("指针变量b的字节为:\n%d\n", sizeof(b));
printf("指针变量的值为:\n%d\n指针变量的地址为:\n%x\n",
*b += 2, b + 2);
int *p;
int *p1;
p = &a[0];
p1 = &a[4];
printf("两指针做减法运算的结果为:\n%d\n", p1 - p);
printf("指针p和p1进行比较后的值为:\n%d", strcmp(p,
p1));
return EXIT_SUCCESS;
}
- c语言函数指针
- C语言函数指针
- c语言 函数指针
- C语言函数指针
- C语言 ---- 指针函数
- C语言函数指针
- C语言函数指针
- C语言函数指针
- c语言函数指针
- C语言函数指针
- C语言函数指针
- c语言函数指针
- C语言--函数指针
- C语言函数指针
- C语言函数指针
- C语言-函数指针
- C语言----函数指针
- c语言函数指针
- sqlserver2005转到sqlserver2000
- linux之sort命令
- java程序设计--孙鑫java无难事Lesson11《Applet》
- 中国人为什么做不出自己的操作系统
- python实现Adapter模式
- C语言函数指针
- 安装新版本python出现乱码的问题
- mysql常用数据类型
- java程序设计--孙鑫java无难事Lesson12《网络编程》
- mysql常用语句
- poj 2425&&poj1338
- mysql之limit
- 韩顺平PHP系列课程--html入门及实战演练
- 有关“龙抬头”