如何在C中将char*指针分配给char变量。

huangapple go评论57阅读模式
英文:

How to assign a char* pointer to a char variable in c

问题

我有一个名为struct Track的结构体,其中有一个变量如下:char artist[81];

我有一个用于创建新音轨的函数:Track *newTrack(char *artist, char *title, int time);

但是这样做是不可能的...

track->artist = artist;

如何将一个字符数组转换成一个字符?

英文:

I have a struct Track with variable like this char artist[81];

I have a function to create a new track 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?

答案1

得分: 1

strcpy函数用法示例:

strcpy(track->artist, artist);

包含头文件:

#include <string.h>
英文:

you can use strcpy ,

    strcpy(track-&gt;artist, artist);

include header

      #include&lt;string.h&gt;

答案2

得分: 1

在这种情况下,你可以这样使用 strcpy

strcpy(track->artist, artist);

但要注意:

为了避免溢出,目标指针所指的数组大小必须足够大,以容纳与源字符串相同的C字符串(包括终止的空字符),并且不应该与源字符串在内存中重叠。

更多详细信息,你可以参考这个有用的链接:strcpy

英文:

In this case you can use strcpy in this way:

strcpy(track-&gt;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.

for more details i leave you a useful link : strcpy

huangapple
  • 本文由 发表于 2020年1月6日 20:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定