您的位置:首页 > 编程语言 > C语言/C++

指针09 - 零基础入门学习C语言49

2011-03-02 00:40 609 查看

第八章:指针09

让编程改变世界

Change the world by program

有关指针的数据类型的小结

[caption id="attachment_167" align="aligncenter" width="300"]

有关指针的数据类型的小结[/caption]

指针运算小结

一、指针变量加(减)一个整数

如:
p++、p--、p+i、p-i、p += i、p -= i等。

二、指针变量赋值

将一个变量地址赋给一个指针变量。

如:
p = &a; (将变量a的地址赋给p)

p = array; (将数组array首元素地址赋给p)

p = &array[i]; (将数组array第i个元素的地址赋给p)

p = max; (max为已定义的函数,将max的入口地址赋给p)

p1 = p2; (p1和p2都是指针变量,将p2的值赋给p1)

三、指针变量可以有空值,即该指针变量不指向任何变量,可以这样表示:p = NULL;

四、两个指针变量可以相减

如果两个指针变量都指向同一个数组中的元素,则两个指针变量值之差是两个指针之间的元素个数

[caption id="attachment_168" align="aligncenter" width="203"]

两个指针变量可以相减[/caption]

五、两个指针变量比较

若两个指针指向同一个数组的元素,则可以进行比较。指向前面的元素的指针变量“小于”指向后面元素的指针变量。

关于void类型

void真正发挥的作用在于:
1. 对函数返回的限定;

2. 对函数参数的限定。

例如:void abc( void );

谈谈 void类型用于指针!

void指针和const指针

ANSI C新标准增加了一种“void”指针类型,即不指定它是指向哪一种类型数据的指针变量。

例如:void *p;

表示指针变量p不指向一个确定的类型数据,它的作用仅仅是用来存放一个地址。

void指针它可以指向任何类型数据。也就是说,可以用任何类型的指针直接给void指针赋值。

但是,如果需要将void指针的值赋给其他类型的指针,则需要进行强制类型转换。

三个例子谈谈const

example01

[codesyntax lang="c"]
#include <stdio.h>

void main(void)
{
const char *str= "Welcome to Fishc.com!nn";
// 这个语句的含义是:声明一个名为str的指针变量,
// 它指向一个字符型常量,初始化str为指向字符串
// "Welcome to Fishc.com!nn"
printf("nn%s", str);
#if (1)
str[0] = 'w';       //这条语句是错误的,但可以改变str指针的值
#endif

str = "I love Fishc.com!nn";   //合法!

printf("nn%s", str);
}

[/codesyntax]

example02

[codesyntax lang="c"]
#include <stdio.h>

void main(void)
{
char * const str = "Welcome to Fishc.com!nn";
// 常量指针是一个固定的指针,不可以改变它的值,但它所指的数据可以改变。

str[0] = 'w';       //合法!

#if( 1 )
str = "I love Fishc.com!nn";   //非法!!
#endif

printf("nn%s", str);
}

[/codesyntax]

example03

[codesyntax lang="c"]
#include <stdio.h>

void main(void)
{
const char * const str = "Welcome to Fishc.com!nn";
// 常量指针是一个固定的指针,不可以改变它的值,但它所指的数据可以改变。

str[0] = 'w';                   //非法!

str = "I love Fishc.com!nn";  //非法!!

printf("nn%s", str);
}

[/codesyntax]

最后我们看看 memcpy

memcpy.c

[codesyntax lang="c"]
/* MEMCPY.C: Illustrate overlapping copy: memmove
* handles it correctly; memcpy does not.
*/

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
*                  12345678901234567890123456789012345678901234567890
*/

void main( void )
{
printf( "Function:tmemcpy without overlapn" );
printf( "Source:tt%sn", string1 + 40 );
printf( "Destination:t%sn", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:tt%sn", string1 );
printf( "Length:tt%d charactersnn", strlen( string1 ) );

/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );

printf( "Function:tmemmove with overlapn" );
printf( "Source:tt%sn", string2 + 4 );
printf( "Destination:t%sn", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:tt%sn", string2 );
printf( "Length:tt%d charactersnn", strlen( string2 ) );

printf( "Function:tmemcpy with overlapn" );
printf( "Source:tt%sn", string1 + 4 );
printf( "Destination:t%sn", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:tt%sn", string1 );
printf( "Length:tt%d charactersnn", strlen( string1 ) );
}

[/codesyntax]

[buy] 获得所有教学视频、课件、源代码等资源打包 [/buy]
[Downlink href='http://kuai.xunlei.com/d/LLLZNJDQJIFH']视频下载[/Downlink]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: