c之函数

函数的定义:
数据类型 函数名([形参说明表])

如果被调函数在主调函数之后,被调函数需声明,一般都是声明和定义

#include <stdio.h>
void fun1(); //函数声明
void main(){
    fun1();
}
//函数定义
void fun1(){
    printf("hello\n");
}
View Code

返回值

#include <stdio.h>
void fun1();
int main(){
    fun1();
    return 0;
}
void fun1(){
    printf("hello\n");
}

[root@localhost fun]# ./a.out 
hello
[root@localhost fun]# echo $?
0

#include <stdio.h>
void fun1();
int main(){
    fun1();
}
void fun1(){
    printf("hello\n");
}

[root@localhost fun]# ./a.out 
hello
[root@localhost fun]# echo $?
6 (hello\n的长度)
View Code

 main的参数

#include <stdio.h>

void main(int argc,char *argv[]){
    int i;
   printf("argc=%d\n",argc);
   //argv[i]!=NULL 替代argc
   for(i=0;i<argc;i++){
       puts(argv[i]);
   }
}
root@centos1 fun]# ls /etc/a*.conf
/etc/asound.conf  /etc/autofs.conf  /etc/autofs_ldap_auth.conf
[root@centos1 fun]# ./a.out  /etc/a*.conf
argc=4
./a.out
/etc/asound.conf
/etc/autofs.conf
/etc/autofs_ldap_auth.conf

 递归调用 斐波那契数

#include <stdio.h>
int fib(int n){
    if(n<1){
        return -1;
    }
    if(n==1 || n==2){
        return 1;
    }
    return fib(n-1)+fib(n-2);
}

void main(int argc,char *argv[]){
    int n;
    int res;
    scanf("%d",&n);
    res=fib(n);
    printf("res=%d\n",res );
}
View Code

 函数与一维数组(传参)

#include <stdio.h>
#include <stdlib.h>

void print_arr(int *p){
    printf("%s:%d\n",__FUNCTION__,sizeof(p) );
}
void print_arr1(int p[]){
    printf("%s:%d\n",__FUNCTION__,sizeof(p) );
}
void main(int argc,char *argv[]){
    int a[]={1,3,5,7,9};
    printf("%s:%d\n",__FUNCTION__,sizeof(a) );
    print_arr(a);
    print_arr1(a);
}
--------------
main:20
print_arr:8
print_arr1:8
View Code

 函数中二维数组传参

#include <stdio.h>
#include <stdlib.h>


#define M 2
#define N 3

void print_arr(int *p,int n){
    int i;
    for(i=0;i<n;i++){
        printf("%d ",p[i]);
    }
    printf("\n");
}
void print_arr1(int p[][N],int m,int n)
// void print_arr1(int (*p)[N],int m,int n)
{
    int i,j;
    printf("sizeof(p)=%d\n",sizeof(p) );
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            printf("%4d ",*(*(p+i)+j) );
        }
        printf("\n");
    }

}

void main(int argc,char *argv[]){
    int i,j;
    int a[M][N]={1,2,3,4,5,6};
    print_arr(&a[0][0],M*N);//实参和形参必须匹配,不能穿a,可传*a
    print_arr(&*a[0],M*N);
    printf("-----------------------\n");
    printf("sizeof(a)=%d\n",sizeof(a) );
    print_arr1(a,M,N);

}
1 2 3 4 5 6 
1 2 3 4 5 6 
-----------------------
sizeof(a)=24
sizeof(p)=8
   1    2    3 
   4    5    6 
#include <stdio.h>
#include <stdlib.h>


#define M 2
#define N 3

void main(int argc,char *argv[]){
    int i,j;
    int a[M][N]={6,5,4,3,2,1};
    printf("a=%p\n", a);
    printf("&a=%p\n",a);
    printf("*a=%p\n",*a);
    printf("a[0]=%p\n",a[0]);
    printf("&a[0]=%p\n",&a[0]);
    printf("*a[0]=%p\n",*a[0]);
    printf("a+1=%p\n",a+1);
    printf("a[0]+1=%p\n",a[0]+1 );

}
------------------------------------
a=0x7ffde1d040d0
&a=0x7ffde1d040d0
*a=0x7ffde1d040d0
a[0]=0x7ffde1d040d0
&a[0]=0x7ffde1d040d0
*a[0]=0x6
a+1=0x7ffde1d040dc
a[0]+1=0x7ffde1d040d4

a是一个行指针,a[0]是一个指向{6,5,4} 1维数组的列指针 ,a[0]就是其数组名

 

#include <stdio.h>
#include <stdlib.h>


#define M 3
#define N 3

void find_num(int (*p)[N],int num)
{
    int i;
    for(i=0;i<N;i++)
    {
        printf("%d ",*(*(p+num)+i));
    }
    printf("\n");
}

void main(int argc,char *argv[]){
    int i,j;
    int a[M][N]={1,2,3,4,5,6,7,8,9};
    find_num(a,1);//4 5 6

}

 

 

 

函数与字符数组

strcpy与strncpy实现

#include <stdio.h>
#include <stdlib.h>

char * mystrcpy(char *dst,const char *src){
    char *ret=dst;
    if(dst != NULL && src !=NULL){
        while((*dst++ = *src++) !='\0');
    }
    return ret;
}
char *mystrncpy(char *dst,const char *src,size_t n){
    int i;
    for(i=0;i<n && (dst[i]=src[i]) && src[i] != '\0';i++){
        ;
    }
    dst[i]='\0';
    return dst;
}

void main(int argc,char *argv[]){
    char str1[]="php is best language";
    char str2[128];
    mystrcpy(str2,str1);
    puts(str2);
    mystrncpy(str2,str1,100);
    puts(str2);
}
View Code

 

posted @ 2018-01-15 10:14  H&K  阅读(229)  评论(0编辑  收藏  举报