-2

could you please help me to understand how to update a pointer adress from a function?

I use a C library (libmpdclient) into my C++ project.

from the C library:

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

my header 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);

my code:

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

I tried so much thinks that I am lost :/

mobidyc
  • 15
  • 6
  • 2
    This has been asked ***many, many, many times.*** C is pass-by-value. If you want to simulate pass-by-reference, you have to use pointers. –  Jun 12 '13 at 10:06
  • 1
    possible duplicate of [C Having a function change the value a pointer represents](http://stackoverflow.com/questions/4844914/c-having-a-function-change-the-value-a-pointer-represents) and of [this question too](http://stackoverflow.com/questions/11044555/problems-changing-a-void-pointer-value-in-c) –  Jun 12 '13 at 10:06
  • Already tried, if I change "song =" to "*song =" I get the following error:invalid use of incomplete type ‘const struct mpd_song’. But I agreee, i self learn C++ and I still don't understand everything. – mobidyc Jun 12 '13 at 10:12
  • 1
    @userXXXX hint: you don't want to change the object pointed to by the pointer. You want to change the pointer itself. –  Jun 12 '13 at 10:14
  • you right, It is what I try to do. – mobidyc Jun 12 '13 at 10:21
  • adding two '**' to the function give me another error : invalid conversion from ‘mpd_song**’ to ‘const mpd_song**’ – mobidyc Jun 12 '13 at 10:22
  • 1
    I think I understand less and less ... :/ – mobidyc Jun 12 '13 at 10:23
  • Could you correct my function and the call to it so I can understand better please? – mobidyc Jun 12 '13 at 10:30
  • `void MpdPlayer::GetSongInfo(struct mpd_song **song, const char *uri)` and `*song = mpd_entity_get_song(entity);` and `GetSongInfo(&myStruct->m_song, m_fileuri);` are what you need. Now **think** about it. –  Jun 12 '13 at 11:16
  • thanks H2C03 but as I said before, this code does not work for me. I don't know why, maybe because the mpd_song struct is const ? – mobidyc Jun 13 '13 at 09:20
  • perhaps. What does your compiler say? You should compile everything with `-Wall`. –  Jun 13 '13 at 09:23
  • he said : invalid conversion from ‘mpd_song**’ to ‘const mpd_song**’ – mobidyc Jun 13 '13 at 09:36
  • yes, everything you adviced was already tested. in fact, i've tested almost all combinations (because i think I don't understand everything correctly) before asking here. so, if I try to understand well. It is not possible to pass a non-initialized pointer to a function and update its address with the const object returned by 'mpd_entity_get_song(entity)' ? – mobidyc Jun 13 '13 at 10:10
  • You can't modify an object if it's const. Therefore if your function declaration looks like `void foo(const T *arg)`, then you can't do `*arg = stuff;` from within the function. –  Jun 13 '13 at 10:13

1 Answers1

0
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);
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • bign thanks Dialecticus ! I still had some problem with the "const" keyword but it is solved now ;) – mobidyc Jun 12 '13 at 10:53
  • @mobidyc beware this code is valid if `mpd_song` object is valid after `mpd_entity_free`. I assumed it is, but if it is not then you have to copy the contents before calling `mpd_entity_free`. – Dialecticus Jun 12 '13 at 12:15