黑马程序员技术交流社区

标题: 关于指针传递 [打印本页]

作者: 张涛的狂怒    时间: 2014-11-21 12:33
标题: 关于指针传递
void f(int *a, int *b)
{
    int t;
    t = *a;
    *a = *b;
    *b = t;
}
int main()
{
    int x = 3, y = 4;
    f(&x, &y);
    printf("%d,%d", x, y);
    return 0;
}
***************************
#include <stdio.h>
void gg(char *s,char *p)
{
    char m[20];
    *m=*s;
    *s=*p;
    *p=*m;
    printf("----\ns==%s\np==%s\n-----\n",s,p);
}
int main()
{
    char s []= "hello c";
    char p []= "hell world";
    printf("s==%s\np==%s\n",s,p);
    gg(s,p);
    printf("s==%s\np==%s\n",s,p);
    return 0;
}
怎样才能通过调用gg改变输出字符串
作者: wang2003    时间: 2014-11-22 21:16
要传递指针的指针才可以。

#include <stdio.h>
void gg(char **s,char **p)
{
    long temp;
   
    temp = *s;
   
    *s = *p;
   
    *p = temp;
    printf("----\ns==%s\np==%s\n-----\n",(char *)s,(char *)p);
}
int main()
{
    char s [20]= "hello c";
    char p [20]= "hell world";
    printf("s==%s\np==%s\n",s,p);
    gg((char **)s,(char **)p);
    printf("s==%s\np==%s\n",s,p);
    return 0;
}
作者: 张涛的狂怒    时间: 2014-11-23 02:42
wang2003 发表于 2014-11-22 21:16
要传递指针的指针才可以。

#include

谢谢明白了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2