WillFu


私信TA

用户名:dotcpp0765438

访问量:178

签 名:

等  级
排  名 29852
经  验 480
参赛次数 0
文章发表 5
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

char *p="My intrests! "

"My intrests! "这个字符串的意义只相当于首地址。

=赋值后,相当于把字符串首地址赋值给了指针变量p。

妙用无穷啊。比数组方便,不用初始化数组元素数量,使用gets()也变安全了。

也可以不使用strcpy()复制字符串了。

////////////////////////////////////////////////

#include<stdio.h>

//#include<string.h> 

struct _INFO

{

    int num;

    char *str;

};

int main() 

{

    struct _INFO A;

    A.num = 2014;

    A.str="Welcome to dotcpp.com";

    //输出字符要使用地址对应的值,注意格式%c

    printf("This year is %d %c\n",A.num,*(A.str+1));

    //输出字符串只需要输出首地址,注意格式%s

    printf("This year is %d %s\n",A.num,A.str);

    

    return 0;

}

/////////////////////////////////////////////////

输出结果:

This year is 2014 e

This year is 2014 Welcome to dotcpp.com

///////////////////////////////////////////////


以上代码是从教程修改来的,验证指针变量用法。教程原来的代码如下:

#include<stdio.h>

#include<string.h> 

struct _INFO

{

    int num;

    char str[256];

};

int main() 

{

    struct _INFO A;

    A.num = 2014;

    strcpy(A.str,"Welcome to dotcpp.com");

    printf("This year is %d %s\n",A.num,A.str);

    return 0;

}


 

0.0分

0 人评分

  评论区