Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108856
  • 博文数量: 40
  • 博客积分: 2058
  • 博客等级: 大尉
  • 技术积分: 409
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-07 16:49
文章分类

全部博文(40)

文章存档

2011年(3)

2010年(17)

2009年(14)

2008年(6)

我的朋友

分类: LINUX

2010-08-10 21:20:51


 这两天和一个学妹对函数参数传值和传地址的问题具体讨论了一下,首先描述一下我们的问题:如何将函数计算结果带出?
我的建议是通过地址传递带出,她则用的是函数返回值。
我用了两种方式具体的代码如下:

example1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory( char **p)
{
    *p = (char *) malloc(6);
        if(*p == NULL){
       printf("malloc error\n");
       }
   /* handling code */
      strcpy( *p, "hello" );
      printf("in fun %s\n",*p); /* str - 1*/

}
void release(char **m)
{
    free(*m);
    *m=NULL;
}

int main( void )
{
     char *str = NULL;
     GetMemory(&str);
     printf("in main: %s\n",str);
     release(&str);
     printf("release:%s",str);
     return 0;
}
example2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory( char *p)
{


   /* handling code */
  strcpy( p, "hello" );
  printf("in fun %s\n",p); /* str - 1*/

}

int main( void )
{
     char *str = NULL;
    str = (char *) malloc(6);
    if(str == NULL){
       printf("malloc error\n");
   }
     GetMemory(str);
     printf("in main: %s\n",str);
     free(str);
     str=NULL;
     return 0;
}

具体含义我就不解释了,只是遵循了一条,指针的实质就是地址,而且我用的传递方式就是地址传递!
先前忘记了释放内存(这是个非常严重的问题),最后补上了,如果还有什么问题请大家多指正!!~:)
这位学妹的代码如下,她使用的是将结果由函数指针带回:


test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* GetMemory( char *p)
{
    p = (char *) malloc(4);


    if(p == NULL){
        printf("malloc error\n");
        return NULL;

    }

    strcpy( p, "hello" );
    printf("in fun: %s\n",p);

    return p;
}
void free_point(char *p) /*其实这个函数没有必要写的,既然GetMemory将分配的地址返回,则可以直接在main函数中将分配的空间feer*/
{
    free(p);
}

/* ********** main *************/

int main( void )
{
    char *str = NULL;

    str=GetMemory( str );
    if(str != NULL) {

        printf("in main: %s\n",str);

        free_point(str);
        str=NULL;
    }

    return 0;
}


总之,在使用malloc进行空间的动态分配和释放的时候如果是局部变量则一定要遵循在哪个函数中申请在哪个函数中释放(如example2.c),如果申请和释放不可能在一个函数中则传递参数时一定要将地址传过去(如:example1.c)。 如果是全局变量,则用完了就因该释放。
最后再总结一下函数结果的带出方式: 函数结果共有 全局变量、函数返回值、传址参数三中带出方式。
通过参数列表的参数传递方式是一种参数显式传递方式,通过全局变量是一种隐式参数传递,一个函数中对全局变量值的改变会影响其他程序的调用,使用全局变量必须注意这个问题。
函数结果需要带出多个值可以采用1.全局变量方式;2.通过地址传递带出(数组方式、结构体方式、指针方式)来实现,看下面的例子

/*
 *
 * Copyright (c) 2009-~ Niu Tao
 *
 * This source code is released for free distribution under the terms of the
 * GNU General Public License
 *
 * Author: Zhao Gejuan
 * Created Time: 2010年08月10日 星期二 17时06分12秒
 * File Name: func.c
 *
 * Description:
 */


#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int max, min;
}Data;
int MIN; /*全局变量*/
int fun1(int a[],int n)
/*通过return返回最大值,通过全局变量MIN带回最小值*/
{
    int i,max;
    max=MIN=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>max) max=a[i];
        if(a[i]<MIN) MIN=a[i];
    }
    return max;
}
int *fun2(int a[],int n)
/*最大最小值放到数组b中通过return 返回*/
{
    static int b[2]; /*此时该数组用static 关键子申明,则其生存其和全局变量相同,变量的值在两次调用之间一直被保持着"*/
    b[0]=b[1]=a[0];
    int i;
    for(i=1;i<n;i++)
    {
        if(a[i]>b[0]) b[0]=a[i];
        if(a[i]<b[1]) b[1]=a[i];
    }
    return b;
}
Data *fun3(int a[],int n)
/*将大小值放在结构体指针p中,通过return 返回*/
{
    Data *p;
    int i;
    p=(Data *)malloc(sizeof(Data)); //指针初始化

    p->max=p->min=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]>p->max) p->max = a[i];
        if(a[i]<p->min) p->min = a[i];
    }
    return p;
}
Data fun4(int a[],int n)
/*将大小值放在结构体p中,通过结构体p带回返回*/
{
    Data p;
    int i;
    p.max=p.min=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]>p.max) p.max = a[i];
        if(a[i]<p.min) p.min = a[i];
    }
    return p;
}
void fun5(int a[],int n,int *p, int *q)
/*用指针带回返回值,指针p指向最大值,指针q指向最小值*/
{
    int i;
    *p=*q=a[0];
    for(i=1;i<n;i++)
    {
        if(*p<a[i])
            *p=a[i];
        if(*q>a[i])
            *q=a[i];
    }
}
int main(int args, char *argv[])
{
    int a[10] ={1,3,9,8,4,2,5,0,7,6},max,*p;
    Data *q;
    Data z;
    int *x,*y;
    x=(int *)malloc(sizeof(int *));
    y=(int *)malloc(sizeof(int *));
    max = fun1(a,10);
    printf("max = %d min=%d\n",max,MIN);

    p=fun2(a,10);
    printf("max = %d min=%d\n",p[0],p[1]);

    q=fun3(a,10);
    printf("max=%d min =%d\n",q->max,q->min);

    z=fun4(a,10);
    printf("max =%d min=%d \n",z.max,z.min);

    fun5(a,10,x,y);
    printf("max = %d min=%d\n",*x,*y);
    free(q);     q=NULL;
    free(x);    x=NULL;
    free(y);    y=NULL;
    return 0;
}


还有什么好的方法,欢迎大家一起讨论!
阅读(872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~