c语言指针数组和字符串比较(C语言中字符串与指针的详细使用)
指针变量也是一种变量,占有内存空间,用来保存内存地址测试指针变量占有内存空间大小,接下来我们就来聊聊关于c语言指针数组和字符串比较?以下内容大家不妨参考一二希望能帮到您!
c语言指针数组和字符串比较
指针变量与其指向内存的关系指针变量也是一种变量,占有内存空间,用来保存内存地址测试指针变量占有内存空间大小。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main0101()
{
char* p = NULL;
char buf[] = "abcde";
printf("p1=%d\n", p);
//改变指针变量的值
p = buf;
printf("p2=%d\n", p);
//指针变量和它指向的内存块是两个不同的概念
p = p 1;//改变指针变量的值,即改变了指针的指向
printf("p3=%d\n", p);
printf("buf=%s\n", buf);
printf("*p=%c\n", *p);//b
printf(" 改变指针指向的内存,并不会改变指针的值\n");
buf[1] = '1';
printf("p4=%d\n", p);
printf("buf2=%s\n", buf);
*p = 'm';
printf("p5=%d\n", p);
printf("buf3=%s\n", buf);
//写内存时,一定要确保内存可写
//char* buf2 = "aaawwweee";//该字符串在文字常量区 不可修改
//buf2[2] = '1';//err
char buf3[] = "wwweerrr";
buf3[1] = 's';//ok
//不允许向NULL和未知非法地址拷贝内存。
char* p3 = NULL;//err
//char* p3 = 0x1111;//err
//给p3指向的内存中拷贝字符串
p3 = buf3;//ok
strcpy(p3, "123");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main0201()
{
char* p = NULL;
char* q = NULL;
char buf[100] = "asdzcx";
p = &buf[0];
printf("p=%d,%c\n", p,*p);
p = &buf[1];
printf("p2=%d,%c\n", p, *p);
printf("\n");
for (int i = 0; i < strlen(buf); i )
{
p = &buf[i];
printf("p3=%d,%c\n", p, *p);
}
q = (char*)malloc(100);
if (q == NULL)
{
return -1;
}
strcpy(q, "qqqwww");
for (int i = 0; i < strlen(q); i )
{
p = q i;
printf("%c\n", *p);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int get_a()
{
int a = 10;
return a;
}
void get_a2(int b)
{
b = 20;
}
void get_a3(int* p)
{
*p = 20;//通过*操作内存
}
void get_a4(int* a1, int* a2, int* a3, int* a4)
{
*a1 = 1;
*a2 = 2;
*a3 = 3;
*a4 = 4;
}
int main0301()
{
int a = 100;
int* p = NULL;
//建立关系
//指针指向谁,就将谁的地址赋值给指针
p = &a;
//通过*操作内存
*p = 22;
/*
通过指针间接赋值
1.两个变量
2.建立关系
3.通过*操作内存
*/
int b = get_a();
printf("b===%d\n", b);
get_a2(b);
printf("b2===%d\n", b);
//如果想通过形参改变实参的值,必须地址传递
get_a3(&b);//函数调用时建立关系
printf("b3===%d\n", b);
int a1, a2, a3, a4;
get_a4(&a1,&a2,&a3,&a4);
printf("a1=%d,a2=%d,a3=%d,a4=%d", a1, a2, a3, a4);
return 0;
}
static void fun2(int* p)
{
p = 0xaabb;
printf("fun2:p=%p\n", p);
}
static void fun3(int** p)
{
*p = 0xaabb;
printf("fun3:p=%p\n", *p);
}
int main0302()
{
//一个变量,应该定义一个怎样类型的指针来保存它的地址
//在原来的基础上加一个*
//int a = 10;
//int* p = &a;
//int** q = &p;
//int********* t = NULL;
//int********** t2 = &t;
int* p = 0x1122;
printf("p1=%p\n", p);
fun2(p);//值传递
printf("p2=%p\n", p);
fun3(&p);//值传递
printf("p3=%p\n", p);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int get_a()
{
int a = 10;
return a;
}
void get_a2(int b)
{
b = 20;
}
void get_a3(int* p)
{
*p = 20;//通过*操作内存
}
void get_a4(int* a1, int* a2, int* a3, int* a4)
{
*a1 = 1;
*a2 = 2;
*a3 = 3;
*a4 = 4;
}
int main0301()
{
int a = 100;
int* p = NULL;
//建立关系
//指针指向谁,就将谁的地址赋值给指针
p = &a;
//通过*操作内存
*p = 22;
/*
通过指针间接赋值
1.两个变量
2.建立关系
3.通过*操作内存
*/
int b = get_a();
printf("b===%d\n", b);
get_a2(b);
printf("b2===%d\n", b);
//如果想通过形参改变实参的值,必须地址传递
get_a3(&b);//函数调用时建立关系
printf("b3===%d\n", b);
int a1, a2, a3, a4;
get_a4(&a1,&a2,&a3,&a4);
printf("a1=%d,a2=%d,a3=%d,a4=%d", a1, a2, a3, a4);
return 0;
}
static void fun2(int* p)
{
p = 0xaabb;
printf("fun2:p=%p\n", p);
}
static void fun3(int** p)
{
*p = 0xaabb;
printf("fun3:p=%p\n", *p);
}
int main0302()
{
//一个变量,应该定义一个怎样类型的指针来保存它的地址
//在原来的基础上加一个*
//int a = 10;
//int* p = &a;
//int** q = &p;
//int********* t = NULL;
//int********** t2 = &t;
int* p = 0x1122;
printf("p1=%p\n", p);
fun2(p);//值传递
printf("p2=%p\n", p);
fun3(&p);//值传递
printf("p3=%p\n", p);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
/*
C语言没有字符串类型,而是通过字符数组模拟
C语言字符串以字符'\0'即数字0结尾
*/
int main0501()
{
//不指定长度,没有结束符0,有多少个元素就有多长
char buf[] = { 'a','b','c' };//3个元素
printf("buf=%s\n", buf);//不加\0的话 末尾乱码
//指定长度,后面没有赋值的元素位置,自动补0
char buf1[100]= { 'a','b','c' };
printf("buf2=%s\n", buf1);
//所谓元素都赋值为0
char buf3[100] = { 0 };
//char buf4[2] = { '1','2','3' };//err 数组越界
char buf5[50] = { '1','a','b','0','7' };
printf("buf5=%s\n", buf5);
char buf6[50] = { '1','a','b',0,'7' };
printf("buf6=%s\n", buf6);// 1ab
char buf7[50] = { '1','a','b','\0','7' };
printf("buf7=%s\n", buf7);// 1ab
//使用字符串初始化,常用此类方式
char buf8[] = "qaaasss";
//strlen:测字符串长度,但不包含数字0与字符'\0'
//sizeof:测数组长度,包含数字0和字符'\0'
printf("strlen=%d,sizeof=%d\n", strlen(buf8), sizeof(buf8));//7 8
char buf9[100] = "qaaasss";
printf("strlen=%d,sizeof=%d\n", strlen(buf9), sizeof(buf9));//7 100
printf("test");
// \012相当于\n
char str[] = "\0129";
printf("%s\n", str);
return 0;
}
int main0502()
{
char buf[] = "aaazzzzssssdddd";
char* p = NULL;
//[]方式
for (int i = 0; i < strlen(buf); i )
{
printf("%c", buf[i]);
}
printf("\n");
//指针法
//数组名字,是数组首元素地址
p = buf;
for (int i = 0; i < strlen(buf); i )
{
printf("%c", p[i]);
}
printf("\n");
for (int i = 0; i < strlen(buf); i )
{
printf("%c",*(p i));//编译器方式
}
printf("\n");
for (int i = 0; i < strlen(buf); i )
{
printf("%c", *(buf i));
}
printf("\n");
//buf 和 p完全等价吗
//p ;//ok
//buf ;//err
//buf只是一个常量,不可修改
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main0601()
{
char src[] = "qqqqqqqqqq";
char dst[100] = { 0 };
int i = 0;
for (; src[i] != 0; i )
{
dst[i] = src[i];
}
//补齐结束符
dst[i] = 0;
printf("%s", dst);
return 0;
}
void my_strcpy(char* dst, char* src)
{
int i = 0;
for (; *(src i) != 0; i )
{
*(dst i) = *(src i);//dst[i]=src[i]
}
// dst[i] = 0;
*(dst i) = 0;
}
void my_strcpy2(char* dst, char* src)
{
while (*src!=0)
{
*dst = *src;
src ;
dst ;
}
*dst = 0;
}
void my_strcpy3(char* dst, char* src)
{
//*dst=*src
//dst ,src
//判断*dst是否为0,为0跳出循环
while (*dst = *src )
{
NULL;//先执行,再自加;先*dst=*src,dst ,src
}
}
//成功返回0,失败返回非零
//1.判断形参指针是否为NULL
//2.不要直接使用形参 以防止将形参的指针指向末尾
int my_strcpy4(char* dst, char* src)
{
if (dst == NULL || src == NULL)
{
return -1;
}
//使用辅助变量将形参的值接来
char* to = dst;
char* from = src;
//*dst=*src
//dst ,src
//判断*dst是否为0,为0跳出循环
while (*from = *to )
{
NULL;//先执行,再自加;先*dst=*src,dst ,src
}
printf("my_strcpy4:dst=%s\n", dst);
return 0;
}
int mai0602n()
{
char src[] = "qqqqqqqqqq";
char dst[100] = { 0 };
int ret = 0;
ret = my_strcpy4(dst, src);
if (ret != 0)
{
printf("my_strcpy4 err:%d\n", ret);
return ret;
}
printf("%s\n", dst);
int i = 0;
int n = i ;
printf("n=%d,i=%d\n", n, i);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main01()
{
char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa
int n = 0;
do
{
p = strstr(p, "zxaa");
if (p != NULL)
{
n ;//累计个数
//重新设置查找起点
p = p strlen("zxaa");
}
else//若没有匹配的字符串跳出循环
{
break;
}
} while (*p!=0);//如果没到结尾
printf("n=%d\n", n);
return 0;
}
int main02()
{
char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa
int n = 0;
while ((p = strstr(p, "zxaa")) != NULL)
{
//能进循环一定有匹配到子串
//重新设置起点位置
p = p strlen("zxaa");
n ;
if (*p == 0)//如果到结束符
{
break;
}
}
printf("n=%d\n", n);
return 0;
}
int my_strstr(char* p, int* n)
{
//两个辅助变量
int i = 0;
char* temp = p;
while ((temp = strstr(temp, "zxaa")) != NULL)
{
//能进循环一定有匹配到子串
//重新设置起点位置
temp = temp strlen("zxaa");
i ;
if (*temp == 0)//如果到结束符
{
break;
}
}
*n = i;
return 0;
}
int main07()
{
char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa
int n = 0;
int ret = 0;
ret = my_strstr(p, &n);
if (ret != 0)
{
return ret;
}
printf("n=%d\n", n);
return 0;
}
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com