英文:
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->artist, artist);
include header
#include<string.h>
答案2
得分: 1
在这种情况下,你可以这样使用 strcpy:
strcpy(track->artist, artist);
但要注意:
为了避免溢出,目标指针所指的数组大小必须足够大,以容纳与源字符串相同的C字符串(包括终止的空字符),并且不应该与源字符串在内存中重叠。
更多详细信息,你可以参考这个有用的链接:strcpy。
英文:
In this case you can use strcpy in this way:
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.
for more details i leave you a useful link : strcpy
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论