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;
}