C 函数返回 gchar 的问题**

发布于 2024-08-12 04:44:51 字数 1971 浏览 4 评论 0原文

我有一个定义如下的函数(在 C 中):

gchar **Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, gchar code)
{
    switch (code)
    {
        case 't':    /* Title */
            return &FileTag->title;
        case 'a':    /* Artist */
            return &FileTag->artist;
        case 'b':    /* Album */
            return &FileTag->album;
        case 'd':    /* Disc Number */
            return &FileTag->disc_number;
        case 'y':    /* Year */
            return &FileTag->year;
        case 'n':    /* Track */
            return &FileTag->track;
        case 'l':    /* Track Total */
            return &FileTag->track_total;
        case 'g':    /* Genre */
            return &FileTag->genre;
        case 'c':    /* Comment */
            return &FileTag->comment;
        case 'p':    /* Composer */
            return &FileTag->composer;
        case 'o':    /* Orig. Artist */
            return &FileTag->orig_artist;
        case 'r':    /* Copyright */
            return &FileTag->copyright;
        case 'u':    /* URL */
            return &FileTag->url;
        case 'e':    /* Encoded by */
            return &FileTag->encoded_by;
        case 'i':    /* Ignored */
            return NULL;
        default:
            Log_Print(LOG_ERROR,"Scanner: Invalid code '%%%c' found!",code);
            return NULL;
    }
}

我试图向 switch 语句添加一个新条件,例如

case 'f':

它将返回 &FileTag->artist 的第一个字符。

我已经搜索了所有互联网来寻找解决方案,但却空手而归。有人有什么想法吗?

更新: 如果有帮助,此功能是应用程序 EasyTag 的一部分。从我通过代码看到的情况来看,这是确定 easytag 文件排序功能的新文件名的地方。我正在尝试添加一个新变量以允许应用程序将音乐分类到如下目录中:<艺术家姓名的首字母>///

我总是有可能看到错误的函数,但据我所知,确实如此。

另一个更新: 我让这个函数按照我想要的方式工作(指针是有趣的小东西),但正如下面指出的,它并没有达到我的预期。感谢大家的帮助和耐心!

I have a function defined as the following (in C):

gchar **Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, gchar code)
{
    switch (code)
    {
        case 't':    /* Title */
            return &FileTag->title;
        case 'a':    /* Artist */
            return &FileTag->artist;
        case 'b':    /* Album */
            return &FileTag->album;
        case 'd':    /* Disc Number */
            return &FileTag->disc_number;
        case 'y':    /* Year */
            return &FileTag->year;
        case 'n':    /* Track */
            return &FileTag->track;
        case 'l':    /* Track Total */
            return &FileTag->track_total;
        case 'g':    /* Genre */
            return &FileTag->genre;
        case 'c':    /* Comment */
            return &FileTag->comment;
        case 'p':    /* Composer */
            return &FileTag->composer;
        case 'o':    /* Orig. Artist */
            return &FileTag->orig_artist;
        case 'r':    /* Copyright */
            return &FileTag->copyright;
        case 'u':    /* URL */
            return &FileTag->url;
        case 'e':    /* Encoded by */
            return &FileTag->encoded_by;
        case 'i':    /* Ignored */
            return NULL;
        default:
            Log_Print(LOG_ERROR,"Scanner: Invalid code '%%%c' found!",code);
            return NULL;
    }
}

I am trying to add a new condition to the switch statement, such as

case 'f':

In which it would return the first character of &FileTag->artist.

I have scoured all of the internet looking for a solution, but have come up empty handed. Anyone have any ideas?

Update:
If it helps, this function is part of the application EasyTag. From what I have seen looking through the code, this is where the new file name for easytag's file sorting feature is determined. I am trying to add a new variable to allow the application to sort music into directories like so: <First letter of artist name>/<Artist>/<Album>/<Tracks>

There is always the chance that I may be looking at the wrong function, but to the best of my knowledge I am.

Another Update:
I got this function working as I wanted (pointers are funny little things), but it does not, as pointed out below, do what I had expected it to. Thanks for all the help and patience!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琴流音 2024-08-19 04:44:51

我不太确定为什么你的代码返回一个指向指针的指针,但这使你想要的操作稍微困难一些。由于您需要返回指针的地址,而不仅仅是指针本身,因此您必须在某处分配该新指针(然后谁来释放它?)。如果您不介意内存泄漏,则可以忽略该问题,只需分配一个指针并返回其地址。或者,您可以创建一个 File_Tag 的新成员,其中包含一个短字符串,其中包含 artist 的第一个字符。

glib 字符串函数 将有助于快速浏览列表显示 g_strdup_printf() 可能有用:

gchar *first = g_strdup_printf("%c", FileTag->artist[0]);

然后分配某个地方来存储 first 并返回它的地址。

更新:使用http://codesearch.google.com我似乎有< a href="http://www.google.com/codesearch/p?hl=zh-CN&sa=N&cd=1&ct=rc#JWizIJK4oqE/easytag-1.1/src/scan.c&q=Scan_Return_File_Tag_Field_From_Mask_Code& l=278" rel="nofollow noreferrer">找到了您要更改的函数的使用位置,并且看起来返回值用于更改返回的字段。

    // Get the target entry for this code
    dest = Scan_Return_File_Tag_Field_From_Mask_Code(FileTag,mask_item->code);

    // We display the text affected to the code
    if ( dest && ( OVERWRITE_TAG_FIELD || *dest==NULL || strlen(*dest)==0 ) )
        ET_Set_Field_File_Tag_Item(dest,mask_item->string);

如果您的代码返回一个包含艺术家姓名第一个字符的新字符串,我不确定您到底想做什么。上面的代码将更改新分配的字符串指向,这对实际的艺术家姓名没有影响。

I'm not quite sure why your code returns a pointer to a pointer, but that makes your desired operation slightly more difficult. Since you need to return the address of a pointer, and not just a pointer itself, you have to allocate that new pointer somewhere (and then who will free it?). If you don't mind the memory leak you can ignore the problem and just allocate a pointer and return its address. Or, you can create a new member of File_Tag that holds a short string that contains the first character of artist.

The glib string functions will be helpful for this, a quick look through the list shows g_strdup_printf() might be useful:

gchar *first = g_strdup_printf("%c", FileTag->artist[0]);

Then allocate somewhere to store first and return the address of that.

Update: Using http://codesearch.google.com I seem to have found where this function you're changing is used, and it looks like the return value is used to change the field that is returned.

    // Get the target entry for this code
    dest = Scan_Return_File_Tag_Field_From_Mask_Code(FileTag,mask_item->code);

    // We display the text affected to the code
    if ( dest && ( OVERWRITE_TAG_FIELD || *dest==NULL || strlen(*dest)==0 ) )
        ET_Set_Field_File_Tag_Item(dest,mask_item->string);

I'm not sure what exactly you want to do if your code returns a new string that contains the first character of the artist name. The above code would change what your newly allocated string points to, which would have no effect on the actual artist name.

夏见 2024-08-19 04:44:51

如果目标是返回一个指向仅包含第一个字母的字符串的指针,并且您不关心线程安全性,那么以下内容应该有效,

static char buf[2];
static char *pbuf = buf;
...
case 'f':
    buf[0] = FileTag->artist[0];
    return &pbuf;

但我发现它很奇怪,您正在返回一个指向指针的指针到“char”,而不是简单地指向“char”的指针。

If the goal is to return a pointer to a string that comprises only the first letter and you don't care about thread-safety, then the following should work

static char buf[2];
static char *pbuf = buf;
...
case 'f':
    buf[0] = FileTag->artist[0];
    return &pbuf;

I find it odd, however, that you're returning a pointer to a pointer to a "char" rather that simply a pointer to a "char".

作业与我同在 2024-08-19 04:44:51

不要在互联网上搜索。自己编码。添加以下内容:

        case 'f':    /* Artist */
            return &FileTag->artist;

假设 &FileTag->artist 的类型为 gchar **

Don't scour the internet. Code it yourself. Add this:

        case 'f':    /* Artist */
            return &FileTag->artist;

That's assuming &FileTag->artist is of type gchar **.

哭了丶谁疼 2024-08-19 04:44:51

如果可能的话,我会获取整个艺术家字符串,然后只需从调用该函数的任何位置访问第一个字母:

gchar **artist = Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, 'f')
printf("%c", *artist[0]);

如果这不是一个选项,您可以为您的角色分配新的内存,但这相当棘手,因为您想要返回一个指向指针的指针。因此,您还必须为该指针分配内存。也许你可以改变这一点...

If possible, I would just get the whole artist string and then simply access the first letter from wherever you call the function:

gchar **artist = Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, 'f')
printf("%c", *artist[0]);

If that is not an option, you could allocate new memory for your character, but that is rather tricky because you want to return a pointer to a pointer. So you would have to allocate also memory for that pointer. maybe you could change that ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文