2

I was hoping someone would be able to tell me if I'm right or wrong, with my following code.

I'm trying to print the returned data from the function mpg123_decoders(), which is of the value char** (or "a NULL-terminated array of the decoders supported by the CPU").

const char ** pdecoders;
pdecoders = mpg123_decoders();
do {
    con_msg(MSG_BAD, "%s\n", *pdecoders);
    pdecoders += 1;
} while(*pdecoders != NULL);

I'm not convinced that my following code does it correctly, as I'm sure there are more available decoders.

6
  • Looks okay to me. How are you sure that there are more available decoders? Commented Mar 1, 2013 at 18:31
  • will pdecoders += 1 really work on a const char **? Commented Mar 1, 2013 at 18:36
  • 1
    I think pdecoders += 1 should work on const char **, because it will be the value of what pdecoders points to (**pdecoders) that is const. Commented Mar 1, 2013 at 18:44
  • I think there are more available decoders, because when I was playing with the code I managed to print another decoder name (one which I haven't been able to get back since). I suppose that could have been due to some other variable, if the code is ok. Thanks. Commented Mar 1, 2013 at 18:45
  • @emil Yes it should. No reason why const should change pointer arithmetic. Commented Mar 1, 2013 at 18:46

1 Answer 1

3

Your code sample doesn't look to bad to me. Just in case there isn't any data at all, I'd change

do {
    con_msg(MSG_BAD, "%s\n", *pdecoders);
    pdecoders += 1;
} while(*pdecoders != NULL);

to

while(*pdecoders != NULL){
    con_msg(MSG_BAD, "%s\n", *pdecoders);
    pdecoders += 1;
}

But the rest looks pretty well.

Although it may sound a bit confusing first due to the way the const keyword ist handled in C but in fact it is the data pointed to that is const and that isn't changed at all, so there should be no problem with that.

Try to read the declaration exactly as it is given and you'll see there is no problem with it:

const char **pdecoders 

Reads as "pdecoders is a pointer to a pointer to a const char".

The problematic case instead would read

char** const pdecoders

As this would turn out to be a "const pointer to a pointer to a char"

Sign up to request clarification or add additional context in comments.

4 Comments

your answer is correct: but just code of mpg123_decoders()
we can use post-increment operator instead of "pdecoders+=1", can't we?
@varine: either ++pdecoders or pdecoders++ would do you could even go with con_msg(MSG_BAD, "%s\n", *pdecoders++); and copletely leave out pdecoders+=1 although it might not be the best style ever.
@mikyra: thank you very much for your help, and for your advice - I'll replace the do-while with a while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.