您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
049第八章 指针09(新版)
发布时间:2021-05-05 12:07:02编辑:雪饮阅读()
从命令行接收参数
#include <stdio.h>
#include <stdlib.h>
void main (int argc, char *argv[])
{
int i;
//输入的参数长度应该为实际长度-1,因为默认的当前源码编译出来的可执行文件名是第一个参数
printf ("the number of string is : %d\n", argc-1);
for(i=1; i < argc; i++)
{
printf ("the string %d is : %s\n", i, argv[i]);
}
}
D:\cproject>m.exe
the number of string is : 0
D:\cproject>m.exe xkws.com i love xkws.com
the number of string is : 4
the string 1 is : xkws.com
the string 2 is : i
the string 3 is : love
the string 4 is : xkws.com
常量指针
常量可以被替换,但不能拆散
有如下程序:
#include <stdio.h>
void main(void)
{
const char *str= "Welcome to xkws.com!\n\n";
printf("\n\n%s", str);
str[0] = 'w';
printf("\n\n%s", str);
}
这里可以看到str是指针,那么对str[0]肯定是失败的,因为指针定义的字符串,一旦定义不能被修改。
所以理所当然编译运行就会报错
D:\cproject>gcc main.c -o m
main.c: In function 'main':
main.c:7:14: error: assignment of read-only location '*str'
str[0] = 'w';
^
但是如果我们将程序修改如:
#include <stdio.h>
void main(void)
{
const char *str= "Welcome to xkws.com!\n\n";
printf("\n\n%s", str);
str = "I love xkws.com!\n\n";
printf("\n\n%s", str);
}
再次编译运行就没有问题了:
D:\cproject>gcc main.c -o m
D:\cproject>m.exe
Welcome to xkws.com!
I love xkws.com!
结论:
常量可以被替换,但不能拆散
还有这里const放在char *str之前表示这里定义了每一个字符是常量,所以上面的代码执行失败,而下面这个代码就执行成功。
我们再来看看如下程序:
#include <stdio.h>
void main(void)
{ //指针变为const
char * const str = "Welcome to xkws.com!\n\n";
str[0] = 'w';
printf("\n\n%s", str);
}
这个程序在windows下编译执行
D:\cproject>gcc main.c -o m
D:\cproject>m.exe
直接就没有结果,那么我们再来看看Linux下的编译执行结果:
[root@localhost ~]# gcc main.c -o m
[root@localhost ~]# chmod +x ./m
[root@localhost ~]# ./m
Segmentation fault
小甲鱼竟然视频中说只是在windows下不能执行(打脸了哈)
这里我觉得最好的解释还是:常量可以被替换,但不能拆散
而字符串就是常量。
那么我们再来看看下面这个实例:
#include <stdio.h>
void main(void)
{ //指针变为const
char * const str = "Welcome to xkws.com!\n\n";
str = "I love xkws.com!\n\n"; //非法!!
printf("\n\n%s", str);
}
D:\cproject>gcc main.c -o m
main.c: In function 'main':
main.c:6:11: error: assignment of read-only variable 'str'
str = "I love xkws.com!\n\n"; //非法!!
可以看到这里直接给指针定义的字符串赋值又失败了,这是因为这里const修饰的是整个指针,而并不是前面那样修改的char类型(相当于每个字符都被修饰为不可更改的常量)
那么同理,下面这个程序将会导致同时抛出两个错误:
#include <stdio.h>
void main(void)
{ //定义字符类型不能写,且指针也不能写
const char * const str = "Welcome to xkws.com!\n\n";
str[0]='w';
str = "I love xkws.com!\n\n";
printf("\n\n%s", str);
}
D:\cproject>gcc main.c -o m
main.c: In function 'main':
main.c:5:10: error: assignment of read-only location '*str'
str[0]='w';
^
main.c:6:11: error: assignment of read-only variable 'str'
str = "I love xkws.com!\n\n";
memcpy内存拷贝
memcpy指的是C和C++使用的内存拷贝函数,函数原型为void *memcpy(void *destin, void *source, unsigned n);函数的功能是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中,即从源source中拷贝n个字节到目标destin中。
可以看看如下官方的一个实例:
/* 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 overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
/*
从源地址拷贝到目标地址:
函数原型
void *memcpy(void *destin, void *source, unsigned n);
参数
destin-- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
source-- 指向要复制的数据源,类型强制转换为 void* 指针。
n-- 要被复制的字节数。
拷贝到目标地址上会以覆盖的形式存放
*/
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );
printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
/*
memmove用于拷贝字节,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,但复制后源内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同。
*/
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}
这里我加了点注释,然后只看了第一个拷贝,其实感觉其它拷贝都差不多,这里不想动脑子了┭┮﹏┭┮。
关键字词:指针,字符,字符串
上一篇:048第八章 指针08(新版)
下一篇:050第九章 预处理01(新版)