gpt4 book ai didi

C - 将 2 个 2D 字符串数组组合并更改为更大的 2D 数组的最简单方法

转载 作者:行者123 更新时间:2023-11-30 14:50:40 26 4
gpt4 key购买 nike

我想为我的程序创建一个函数,它采用 2 个 2D 数组,第一个包含艺术家的姓名,第二个包含所述艺术家的歌曲,将艺术家姓名放在数组的开头,然后是一个“-”,然后是歌曲名称:

Artist1 - Song 1.

Artist1 - Song 2.

..

..

Artist4 - Song 3.

我已经为这样的函数创建了原型(prototype):

int amalgamateArtistsAndSongs (char amalgamatedSongs[][163],char artists[][80],
char songsOfAnArtists[][80],int numOfSongs[],int pos);

数组声明:

    //The array containing artists names
char artists[4][80];
//The array containing the sorted artists
char sortedArtists[4][80];
//Songs for Artist 1
char songsArtist1[3][80];
//Songs for Artist 2
char songsArtist2[3][80];
//Songs for Artist 3
char songsArtist3[3][80];
//Songs for Artist 4
char songsArtist4[3][80];
//Array which holds amalgamated artist and song names -> (Artists - Song Name).
char amalgamatedSongs[12][163];
//The total number of artists (Note it can be less than 4)
int numOfArtists = 0;
//The total number of songs for each artist (Note that less than 3 songs can be provided for each artist)
int numSongsPerArtist[4] = {0,0,0,0};

我已经包含了 int numSongsPerArtist; 因为可能会出现一个艺术家的歌曲少于 3 首的情况,以避免 Artistx - “blank” 出现在 amalgamatedSongs[][]

我尝试使用 strcpy()strcat() 创建此函数,但它似乎没有按照我希望的方式工作:

int amalgamateArtistsAndSongs (char amalgamatedSongs[][163],char artists[][80],char songsOfAnArtists[][80],int numOfSongs[],int pos)
{
int i;

for (i=0;i<numOfSongs;i++)
{
strcpy(amalgamatedSongs[pos],artists[]);
strcat(amalgamatedSongs[pos]," - ");
strcat(amalgamatedSongs[pos],songsOfAnArtists[i]);
strcat(amalgamatedSongs[pos],"\0");
pos++;
}

return pos;
}

我使用 int pos; 来指示该函数的下一次调用,其中 amalgamatedSongs[][] 中该函数的上一次调用停止了(其中最后一位艺术家 - 歌曲被放置)。

这就是函数的调用方式

amalgamateArtistsAndSongs(amalgamatedSongs,artists[1],
songsArtist2,numSongsPerArtist[1],pos);

澄清一下:

如果

artists[][80] = {"artist1","artist2","artist3","artist4"};
songsArtist1[][80] = {"a","b","c"};
songsArtist2[][80] = {"aa","bb","cc"};
songsArtist3[][80] = {"aaa","bbb"};
songsArtist4[][80] = {"aaaa"};

然后(使用 amalgamateArtistsAndSongs() 函数后)

amalgamatedSongs[][163] = {"artist1 - a","artist1 - b","artist1 - c",
"artist2 - aa","artist2 - bb","artist2 - cc","artist3 - aaa","artist3 - bbb",
"artist4 - aaaa"};

最佳答案

这个函数

int amalgamateArtistsAndSongs (char amalgamatedSongs[][163],
char artists[][80],char songsOfAnArtists[][80],
int numOfSongs[],int pos)
{
int i;

for (i=0;i<numOfSongs;i++)
{
strcpy(amalgamatedSongs[pos],artists[]);
strcat(amalgamatedSongs[pos]," - ");
strcat(amalgamatedSongs[pos],songsOfAnArtists[i]);
strcat(amalgamatedSongs[pos],"\0");
pos++;
}

return pos;
}

不正确:numOfSongs是一个指向数组的指针,for循环会一直循环直到 i获取整数值numOfSongs 的地址的数量指向你肯定会溢出 amalgamatedSongs 。编译器肯定已经对此发出警告。正确的条件应该是i < numOfSongs[pos] .

还有

 strcpy(amalgamatedSongs[pos],artists[]);

是一个语法错误,你应该得到这样的信息:

error: expected expression before ‘]’ token
strcpy(amalgamatedSongs[pos],artists[]);
^

你可能想要

 strcpy(amalgamatedSongs[pos], artists[pos]);

这个

   strcat(amalgamatedSongs[pos],"\0");

不需要,因为 strcat写道'\0' -终止字节。

本系列的一个问题strcat是你不知道整个弦是否适合 amalgamatedSongs[pos] ,你可能会溢出缓冲区。您必须使用 strncpystrncat并手动设置 '\0' - 在每次调用之间终止。这是很多工作,我会使用连接字符串 snprintf :

int amalgamateArtistsAndSongs (char amalgamatedSongs[][163],
char artists[][80],char songsOfAnArtists[][80],
int numOfSongs[],int pos)
{
int i;

for (i=0;i<numOfSongs;i++)
{
snprintf(amalgamatedSongs[pos], 163, "%s - %s",
artists[pos], songsOfAnArtists[i]);

pos++;
}

return pos;
}

snprintf最多可写入 163 个字符包括'\0' - 终止字节。如果艺术家 - 歌曲组合太长,需要 162 个字符,snprintf只会写162个字符并设置'\0' -终止字节,因此不会溢出缓冲区。

man printf

#include <stdio.h>
int snprintf(char *str, size_t size, const char *format, ...);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.

关于C - 将 2 个 2D 字符串数组组合并更改为更大的 2D 数组的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48892823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com