指针、数组1

本文探讨了C语言中的指针和数组操作,包括字符串操作、内存拷贝、指针加减以及静态变量的作用。示例代码中展示了如何使用指针访问和修改数组元素,以及静态变量在函数调用中的行为。同时,文章提到了strlen函数在不同情况下的应用,并解释了指针与数组的关系。通过对文件路径的指针操作,说明了指针变量与原始数组之间的关联性。
摘要由CSDN通过智能技术生成

在这里插入图片描述

代码及执行结果2
在这里插入图片描述

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

/* 函数声明 */
void func1(void);
 
static int count=10;        /* 全局变量 - static 是默认的 */
 
int main()
{
	char *p="zhang";
	char *q;
	p=&p[1];
    printf("%s,%c,%c\n",p,*p,*(p+2));
	
	memcpy(q,p+1,3);
	printf("%s\n",q);
	
	
	//指针+数组+scanfd76
char str[60]={0x31,0x32,0x33,0x34,0x35,0x36}, *sp=str;//将数组的首地址str赋给指针变量sp
//scanf("%s",sp);
	//sp++;
	sp=&str[3];
printf("%s,%c,%c\n",sp,*sp,*(sp+2));//pointer,p,n
printf("%s,%c,%c\n",str,str[0],str[3]);//pointer,p,n
  while (count--) {
      func1();
  }
  return 0;
}
 
void func1(void)
{
/* 'thingy' 是 'func1' 的局部变量 - 只初始化一次
 * 每次调用函数 'func1' 'thingy' 值不会被重置。
 */                
  static int thingy=5;
  thingy++;
  printf(" thingy 为 %d , count 为 %d\n", thingy, count);
	
	
}

错误记录:

  char ang_buf[] = {0x31,0x32};
  char head_buf[] ={0x34,0x31};
  char *str = NULL;
#if 0
/****这里用法典型错误:ang一个字节时打印出大长度为3如下,改到2个字节后计算长度正常****/
  char ang[]={0x39};
  crc_len = strlen( ang);
  printf("crcout len 9in  =%d\n", crc_len);


/****这以内这样正常:ang一个字符时打印出大长度为1如下****/
  char ang[]="9";
  crc_len = strlen( ang);
  printf("crcout len 9in  =%d\n", crc_len);
#endif

在这里插入图片描述

#include <stdio.h>  
#include <string.h>  
  
int main() {  
    char zf_send_at_path[100] = "/path/to/your/file.txt"; // 假设这是你的路径  
    char *filepath;  
  
    // 直接将 zf_send_at_path 的地址赋给 filepath  
    filepath = zf_send_at_path;  
  
    // 现在 filepath 指向 zf_send_at_path 中的数据  
    printf("The file path is: %s\n", filepath);  
  
    // 注意:如果你之后修改了 zf_send_at_path 的内容,filepath 指向的内容也会改变  
    // 因为 filepath 只是指向了 zf_send_at_path 的内存地址  
  
    // 例如:  
    strcpy(zf_send_at_path, "/another/path/to/file.txt");  
    printf("After modification, the file path is: %s\n", filepath);  
  
    return 0;  
}

在这里插入图片描述

#include <stdio.h>  
  
int main() {  
    char zf_send_at_path[100] = "/path/to/your/file.txt"; // 初始化数组  
    char *filepath; // 声明一个字符指针  
  
    // 将数组首元素的地址赋给字符指针  
    filepath = zf_send_at_path;  
  
    // 现在filepath指向zf_send_at_path数组的内容  
    printf("通过filepath访问到的路径是: %s\n", filepath);  
  
    // 你可以通过filepath来访问和修改zf_send_at_path的内容(但要注意不要越界)  
    // 例如,打印第一个字符  
    printf("第一个字符是: %c\n", *filepath);  
  
    // 修改第一个字符(例如改为'T')  
    *filepath = 'T';  
  
    // 打印修改后的数组内容  
    printf("修改后,通过zf_send_at_path访问到的路径是: %s\n", zf_send_at_path);  
  
    return 0;  
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值