C语言-char_*a与char_a[ ]的区别

目录:

一、char *a与char a[ ]的区别

二、C语言中数组的赋值

1、自己将数组中的内容赋值

2、使用string.h标准库中的函数:strcpy

三、附录


一、char *a与char a[ ]的区别

char *a = "hello" 中的a是指向第一个字符‘h'的一个指针;

char a[20] = "hello" 中数组名a也是执行数组第一个字符‘h’的指针。

但二者并不相同,看如下把两个字符串相加(strcat函数见附录)的实例:

结果:

对比:

结果:

把字符串加到指针所指的字串上去,出现段错误,本质原因:*d="0123456789"存放在常量区,是无法修改的。而数组是存放在栈中,是可以修改的。两者区别如下:

1、读写能力

char *a = "abcd";  //"abcd"存放在常量存储区,通过指针只可以访问字符串常量,而不可以改变它

char a[20] = "abcd";  //"abcd"存放在,可以通过指针去访问和修改数组内容

2、赋值时刻

char *a = "abcd";  //编译时就确定了(因为是常量)

char a[20] = "abcd";  //运行时确定

3、存取效率

char *a = "abcd";  //存于常量存储区上的数组比指针所指向字符串快,因此慢

char a[20] = "abcd";  //存于上,因此

4、注意

1)char a[] = "01234",虽然没有指明字符串的长度,但是此时系统已经开辟好了,就是大小为6('0' '1' '2' '3' '4' '5' '\0')。

2)另外注意strlen(a)是不计‘\0’。

注:内存分配方式详见C语言使用相关汇总216、堆、栈、存储区

二、C语言中数组的赋值

若已声明某变量如a为数组(比如char数组),则不能用=号给它赋值(除了初始化之外)!也就是说除了初始化,再不能将数组放在=号左边!若想对char数组另外赋值见下两例:

1、自己将数组中的内容赋值

#include "stdio.h"

void main(void)
{
    char *s="hello";
    char a[10]={"0"};
    int i;
    
    for(i=0;i<strlen(s);i++)
        a[i] = s[i];
    
    printf("%s\n",a);
}

因为 char *s="hello"; 就等于声明了s[] == "hello";

2、使用string.h标准库中的函数:strcpy

#include "stdio.h"
#include "string.h"

int main(void)
{
  char *s="hello";
  char a[10]={"0"};

  strcpy(a,s); //拷贝s字符串到a中
  return 0;
}

三、附录

strcat原型:extern char *strcat(char *dest, const char *src);

strcat功能:将两个char类型连接,src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。返回指向dest的指针。


无知(没有认知)从某种意义来说是一种罪,盲目的努力也是在消耗生命,资源、选择与方法比努力更为重要。觉得不错,动动发财的小手点个赞哦!

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<windows.h> #include<string.h> #include<stdlib.h> #include<math.h> typedef struct character_name { char name[100]; struct character_name* next; }char_name; typedef struct character_title { char title[100]; struct character_title* next; }char_title; typedef struct character_identity { char identity[100]; struct character_identity* next; }char_iden; typedef struct character_profession { char profession[100]; int level; struct character_profession* next; }char_pro; typedef struct character_ability { char ability[100]; char explain[1000]; struct character_ability* next; }char_abi; typedef struct character_race { char race[100]; struct character_race* next; }char_race; typedef struct character_information { int age; int gender; char_name* _name; char_title* _title; char_iden* _iden; char_pro* _pro; char_abi* _abi; char_race* _race; struct character_information* next; }char_inf; char_inf* initialization() { char_inf* node = (char_inf*)malloc(sizeof(char_inf)); node->_name = (char_name*)malloc(sizeof(char_name)); node->_title = (char_title*)malloc(sizeof(char_title)); node->_iden = (char_iden*)malloc(sizeof(char_iden)); node->_pro = (char_pro*)malloc(sizeof(char_pro)); node->_abi = (char_abi*)malloc(sizeof(char_abi)); node->_race = (char_race*)malloc(sizeof(char_race)); return node; } char_inf* ceshi, * current, * end; char_name* name_current, * name_end; char_title* title_current, * title_end; char_iden* iden_current, * iden_end; char_pro* pro_current, * pro_end; char_abi* abi_current, * abi_end; char_race* race_current, * race_end; int main() { ceshi = initialization(); ceshi->age = 666; ceshi->gender = 1; name_current = ceshi->_name; strcpy(name_current->name, "ceshi_name_1"); name_current->next = (char_name*)malloc(sizeof(char_name)); name_current = name_current->next; name_current->next = NULL; strcpy(name_current->name, "ceshi_name_2"); strcpy(ceshi->_title->title, "ceshi_title"); strcpy(ceshi->_iden->identity, "ceshi_identity"); strcpy(ceshi->_pro->profession, "ceshi_profession"); strcpy(ceshi->_abi->ability, "ceshi_ability"); strcpy(ceshi->_abi->explain, "ceshi_ability_explain"); strcpy(ceshi->_race->race, "ceshi_race"); ceshi->_pro->level = 666; name_current = ceshi->_name; printf(" Age: %d\n", ceshi->age); printf(" Gender: "); if (ceshi->gender == 0) printf("woman\n"); else printf("male\n"); while (name_current != NULL) { printf(" Name: %s\n", name_current->name); name_current = name_current->next; } printf(" Title: %s\n Identity: %s\n Profession: %d\n Ability: %s\n Ability_Explain: %s\n Race: %s\n", ceshi->_title->title, ceshi->_iden->identity,ceshi->_pro->level,ceshi->_abi->ability,ceshi->_abi->explain,ceshi->_race->race); return 0; } 怎样可以消除取消对NULL指针的引用这个警告
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上电路设计

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值