char *s 和 char s[] 的区别小结

本文详细解析了C语言中字符数组与字符指针的区别及应用场景,包括内存模型、赋值操作等方面,并对比了二者在不同情况下的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误。一时也不能说得很明白,网上也搜了一下相关文章发现一些写的比较好的,综合了一下当教育资料备用。

 

char *s1 = "hello";
char s2[] = "hello";

 

【区别所在】

char *s1 的s1,而指针是指向一块内存区域,它指向的内存区域的大小可以随时改变,而且当指针指向常量字符串时,它的内容是不可以被修改的,否则在运行时会报错。
char s2[]的s2 是数组对应着一块内存区域,其地址和容量在生命期里不会改变,只有数组的内容可以改变

 

【内存模型】
       +-----+     +---+---+---+---+---+---+
   s1: |  *======> | h | e | l | l | o |\0 |
       +-----+     +---+---+---+---+---+---+
       +---+---+---+---+---+---+
   s2: | h | e | l | l | o |\0 |
       +---+---+---+---+---+---+

 

场景一)
char *s1 = "hello";
char s2[] = "hello";
s2=s1;  //编译ERROR
s1=s2;  //OK

 

分析:s2其地址和容量在生命期里不能改变

 

场景二)
char s2[] = "hello";
char *s1 = s2;  //编译器做了隐式的转换 实际为&s2

char *s1 = &s2;

 

分析:以上两个指针复值完全等价,由于编译器会做这个隐式转换也容易导致初学者误认为 char *s 与char s[]是一回事。
      另用第二种在一些编译器甚至会报警告信息。

 

场景三)
char *s1 = "hello";
char s2[] = "hello";
s1[0]='a';  //×运行ERROR( 这一句好像在一些的编译器不会出错,原因待查)
s2[0]='a';  //OK

 

分析:运行时会报错,原因在于企图改变s1的内容,由于s1指向的是常量字符串,其内容是不可修改的,因此在运行时不会通过。而s2指向的是变量区字符串,可以修改。

 

场景四)
让我们来给一个指针的指针赋值,在使用某些含char**参数的函数时会用到,场景二的增强版。
    char *s1="hello";
    char s2[]="hello";
    char *s3=s2;       //★注意这句必须要★
    char **s4=&s3;   //s2(char[])要用两步才能完成赋值
    char **s5=&s1;   //s1(char*) 只需一步
    printf("s4=[%s]\n",*s4);//打印结果:s4=[hello]
    printf("s5=[%s]\n",*s5);//打印结果:s5=[hello]

 

分析:这个例子应当说最能反映出char *与char []的差异,但是由于使用场合不多,新人尤其需要注意。

 

下面是一些char *s1 和 char s2[]相同的地方(同样编译器对char[]做了隐式变化):
1)作为形参完全相同
如:
   void function(char *s1);
   void function(char s1[]);

 

2)只读取不修改的时候
如:
    char *s1="hello";
    char s2[]="hello";
    printf("s1[1]=[%c]\n",s1[1]);   //s1[1]=[e]
    printf("s2[1]=[%c]\n",s2[1]);   //s2[1]=[e]
    printf("s1=[%s]\n",s1);         //s1=[hello]
    printf("s2=[%s]\n",s2);         //s2=[hello]

/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include <string.h> #include <stdio.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ // 自定义函数声�? void SendCommand(const char *cmd, uint32_t timeout); void SendPDU_SMS(const char *sca, const char *phone, const char *unicode_msg); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ // 短信中心号目标号码 #define SMSC "+8613010811500" // 短信中心号码 #define TARGET_NUMBER "17313800767" // 目标手机�? #define UNICODE_MSG "4F60597D" // "你好"的Unicode编码 /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ // 等待模块初始�? HAL_Delay(3000); // 发�?�短�? SendPDU_SMS(SMSC, TARGET_NUMBER, UNICODE_MSG); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ // 短信发�?�主函数 void SendPDU_SMS(const char *sca, const char *phone, const char *unicode_msg) { /* 第一部分:构造PDU数据 */ // 短信中心号转换(+8613010811500 -> 683101381010F5�? char pdu[100]; sprintf(pdu, "0011000D9168%02X%s0008AA%02X%s", 0x31, // 目标号码处理参数 "31801010F5",// 实际短信中心号处理结�? strlen(unicode_msg)/2, // 数据长度 unicode_msg); /* 第二部分:AT指令发�?? */ SendCommand("AT", 1000); // 模块�?�? HAL_Delay(1000); SendCommand("AT+CSCS=\"UCS2\"",1000); // 设置Unicode编码 HAL_Delay(1000); SendCommand("AT+CMGF=0", 1000); // PDU模式 HAL_Delay(1000); // 发�?�PDU数据 char cmgs_cmd[20]; sprintf(cmgs_cmd, "AT+CMGS=%d", (strlen(pdu)/2 - 1)); // 计算PDU长度 SendCommand(cmgs_cmd, 1000); HAL_Delay(500); // 发�?�PDU内容 HAL_UART_Transmit(&huart2, (uint8_t*)pdu, strlen(pdu), 1000); HAL_UART_Transmit(&huart2, (uint8_t*)"\x1A", 1, 1000); // Ctrl-Z结束 } // 通用AT指令发�?�函�? void SendCommand(const char *cmd, uint32_t timeout) { HAL_UART_Transmit(&huart2, (uint8_t*)cmd, strlen(cmd), timeout); HAL_UART_Transmit(&huart2, (uint8_t*)"\r", 1, timeout); // 发�?�回�? } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ 这代码能发送中文短信吗
最新发布
05-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值