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;
}