简体   繁体   English

c - 如何将char *指针分配给c中的char变量

[英]How to assign a char* pointer to a char variable in c

I have a struct Track with variable like this char artist[81];我有一个带有变量的struct Track ,就像这个char artist[81];

I have a function to create a new track Track *newTrack(char *artist, char *title, int time);我有一个创建新曲目的功能Track *newTrack(char *artist, char *title, int time);

But doing this is not possible..但这样做是不可能的..

track->artist = artist;

How can I transform a char array into a char?如何将char数组转换为char?

you can use strcpy ,你可以使用 strcpy ,

    strcpy(track->artist, artist);

include header包括标题

      #include<string.h>

In this case you can use strcpy in this way:在这种情况下,您可以通过以下方式使用strcpy

strcpy(track->artist, artist);

but pay attention:但要注意:

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.为避免溢出,destination 指向的数组的大小应足够长以包含与 source 相同的 C 字符串(包括终止空字符),并且不应与 source 在内存中重叠。

for more details i leave you a useful link : strcpy有关更多详细信息,我给您留了一个有用的链接: strcpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM