简体   繁体   中英

How to store string from struct into a 2D array

Say I have char Name[100][20] and a struct text with the char variable name1 . I have a txt file that contains a name and for which I used sscanf to store it to text.name1 . Now I want to store that string into the first row of the 2D array so I did Name[0][20]={"%s",text.name1}; but it results to an error that says expected expression before '{' token . Can someone please tell me how to fix this:(

With Name[0][20]={"%s",text.name1}; you're trying to initialize the array.
However, {"%s",text.name1}; is not a valid array initializer. To copy text into an array, you can use something like memcpy() , or sprintf() :

sprintf(Name[0], "%s", text.name1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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