无法将指向字符的指针分配给字符数组 [英] Unable to assign a pointer to character to array of chararacters
本文介绍了无法将指向字符的指针分配给字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要分配一个指向字符数组的指针:
I need to assign a pointer to an array of characters:
char str1[17];
char* h="hey";
str1 = h;
但我收到此错误:error C2440: '=' : cannot convert from 'char *' to 'char [17]'
.
我认为数组名称与指向字符的指针相同,数组名称是数组第一个字符的地址,因此将指向字符的指针分配给数组名称会起作用.
I was thinking that an array name is the same as a pointer to a character and array name is the address of the first character of the array so assigning a pointer to a char to array name would work.
我不知道为什么会出现错误.
I don't know why i get the error.
我该如何解决?
推荐答案
你不能让 str1
指向不同的地址.
You cannot make str1
point to a different address.
你可以做的是使用strncpy
将h
的内容复制到str1
中.
What you can do is use strncpy
to copy the contents of h
into str1
.
这篇关于无法将指向字符的指针分配给字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文