c++ - 从函数初始化对象地址

标签 c++ c pointers

您能帮我了解如何从函数更新指针地址吗?

我在我的 C++ 项目中使用了 C 库 (libmpdclient)。

来自 C 库:

const struct mpd_song *     mpd_entity_get_song (const struct mpd_entity *entity)

我的标题原型(prototype):

typedef struct {
  struct mpd_song * m_song; // from a C library
  ....                      // not important stuff
}
void GetSongInfo(const struct mpd_song * m_song, char* uri);

我的代码:

void MpdPlayer::GetSongInfo(const struct mpd_song * song, char* uri)
{
  struct mpd_entity * entity;
  mpd_send_list_meta(m_connection, *uri);
  song = mpd_entity_get_song(entity);
  mpd_entity_free(entity);

  printf("song adress: %p\n", song); // OK the result is 0x370e6e0
  printf("song duration: %u\n", mpd_song_get_duration(song)); // OK I can get the time
}

GetSongInfo(myStruct->m_song, m_fileuri);
printf("myStruct->m_song adress : %p\n", si->m_song); // FAIL the result is : nil

我尝试了很多,但我认为我迷失了:/

最佳答案

struct mpd_song * MpdPlayer::GetSongInfo(char* uri)
{
  struct mpd_entity * entity;
  mpd_send_list_meta(m_connection, *uri);
  struct mpd_song * song = mpd_entity_get_song(entity);
  mpd_entity_free(entity);

  printf("song adress: %p\n", song);
  printf("song duration: %u\n", mpd_song_get_duration(song));

  return song;
}

myStruct->m_song = GetSongInfo(m_fileuri);

关于c++ - 从函数初始化对象地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062756/

相关文章:

c - linux内核模块中的每个任务数据结构

c - C中的文件截断和填充

c++ - 使用指针

无法创建结构列表

c++ - 以结构为参数的指向结构的例程指针

c++ - 在父窗口所在屏幕上显示子窗口

c++ - 如何将 char 和 char* 附加到现有字符串?

c - 为什么 gcc 打印 "Segmentation fault: 11"?

c++ - 二进制搜索没有返回正确的值

c++ - 如何使用 Eigen 3 表达 "<array-of-true-or-false> = <array> <= <scalar>"?