C programe (The second day of the training of cognizant )

本文通过几个具体的C语言程序实例介绍了指针的基本用法,包括如何使用指针交换变量值、遍历并显示数组元素等内容,并进一步探讨了字符数组的处理方法以及字符串操作函数的实现。
note
tip: all the address are integer  2byes
for example one
	     float b=10.5   4bytes;
	     float *pd2     2bytes;
for example two
 
practice one
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main(){
	clrscr();
	int a=10,b=20;
	swap(&a,&b);
	printf("a=%d",a);
	printf("b=%d",b);
	getch();
}
void swap(int *a,int *b){
	int tempt;
	tempt=*a;
	*a=*b;
	*b=tempt;
}
practice two
#include<stdio.h>
#include<conio.h>
void display(int *a,int size);
int i=0,j=0;
void main(){
	clrscr();
	printf("Hello World");
	getch();
}
void display(int *a,int size){
	for( i=0;i<size;i++)
	printf("%d",*a);
	//distinguish  the int a=10;a++; because the point is 2 bytes;
	a++;
}

 practice three
note
image char[] a[]="David"; the array end of '\0' when print out the array of a automatic run to the end of '\0'
#include<stdio.h>
#include<conio.h>
#include<string.h>
void display(int *a,int size);
int i=0,j=0;
int mylength(char s[]);
void main(){
	clrscr();
	int a[]={10,11,15},length;
	char name[]="David";
	display(a,3);
         //printf("name=%s",name);
	printf("\nHello World");
	printf("\n mylenght:%d",mylength(name));
	//the teacher example using the method
	puts("Enter name");
	gets(name);
	puts(name);
	length=strlen(name);
	printf("Length=%d",length);
	getch();
}
void display(int *a,int size){
	for( i=0;i<size;i++) {
		printf("%d\n",*a);
		//distinguish  the int a=10;a++; because the point is 2 bytes;
		a++;
	}
}
// return the mycount name length;
int mylength(char s[]){
  int t=0;
  while(s!='\0'){
	t++;
	s++;
  }
   return t;
}
homework one
#include<stdio.h>
#include<conio.h>
#include<string.h>
void mycpy(char *a,char *b);
void mycat(char *a,char *c,char *d);
void main(){
 clrscr();
 char a[10],b[10],c[10],d[10],e[10];
 puts("please enter copy string:");
 gets(a);
 puts("please enter cat first String: ");
 gets(c);
 puts("please enter cat second String: ");
 gets(d);
 mycpy(a,b);
 printf("copy string is :%s\n",b);
 mycat(c,d,e);
 printf("cat string is :%s",e);
 getch();
}
void mycpy(char *a,char *b ){
  while(*a!='\0'){
   *b=*a;
   b++;
   a++;
  }
  *b='\0';
}

void mycat(char *a,char *b,char *c){
 while(*a!='\0'){
   *c=*a;
   c++;
   a++;
 }
 while(*b!='\0'){
  *c=*b;
  b++;
  c++;
 }
 *c='\0';
}
homework two
#include<stdio.h>
#include<conio.h>
struct employee{
char *name;
int id;
int salary;
};
employee Highest(employee e[],int size);
void main(){
 clrscr();
 employee e[3],t;
 /*
 e[0].name="david"; e[1].name="Jak"; e[2].name="tom";
 e[0].id=1010;  e[1].id=1001;e[2].id=1002;
 e[0].salary=250;e[1].salary=350;e[2].salary=500;
 */
 for(int i=0;i<10;i++){
   puts("Enter you name:");
   scanf("%s",e[i].name);
   puts("Enter you id:");
   scanf("%d",&e[i].id);
   puts("Enter you salary:");
   scanf("%d",&e[i].salary);
 }
 t=Highest(e,2);
 printf("the highest salary name:%d,",t.name);
 printf("id:%d,",t.id);
 printf("salary,",t.salary);
 getch();
}
employee Highest(employee e[],int size){
  int i=0;
  employee t=e[0];
  for(i=0;i<size;i++){
     if(e[i].salary>e[0].salary)
     t=e[i];
  }
  return t;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值