-1

Im learning C so i have a little problem.

How to print: unsigned char *tlv_buffer = NULL;

In main function:

unsigned char *tlv_buffer = NULL;
    int size = 1;
    int len = 0;
    int result;

    tlv_buffer = BKS_MALLOC(size);

    result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF04, 2,
            "\x34\x56");
    result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF81, 3, "ref");
    BKS_TRACE("-------- success : %d --------- \n", result);

    BKS_TRACE("======== %u =======", &tlv_buffer);

(I cannot see what happens in append_bertlv_data)

It should print df 04 02 34 56 df 81 03 72 65 66 , but it does not show like that.

My result is 3204447612

Dulguuntur
  • 43
  • 3
  • 9
  • 1
    *"Question is how to print *tlv_buffer?"* Print how? Your question is unclear. Please [edit] it, and provide more information. – user694733 Nov 28 '16 at 08:49
  • What does the buffer contain and how do you want it displayed? – Klas Lindbäck Nov 28 '16 at 08:51
  • 1
    There is a plethora of duplicates to choose an answer from. – Paul Rooney Nov 28 '16 at 08:56
  • Does `append_bertlv_data` allocate more space when needed? If not then you allocate only 1 byte, and it's not enough for your result. – user694733 Nov 28 '16 at 09:07
  • 2
    What is `BKS_TRACE`? If it's wrapper to `printf`, then `%u` is not correct conversion specifier for printing the address. Also, you cannot print array with single call, you need to loop over the elements individually. – user694733 Nov 28 '16 at 09:11
  • the correct format for addresses is [`%p`](http://stackoverflow.com/q/9053658/995714) – phuclv Nov 28 '16 at 09:12
  • BKS_TRACE is wrapper to printf – Dulguuntur Nov 28 '16 at 09:42
  • Do you even have the prototype for append_bertlv_data(). Is it returning a pointer. It looks more likely that it is reallocating space on tlv_buffer as you're sending down a reference to the pointer itself (so it is working on your buffer pointer). If so all looks well, BUT for some reason you're doing a printf on &tlv_buffer. You need to printf using tlv_buffer not %tlv_buffer. But this will only give you the address it points to. You need to step through the memory by dereferencing the pointer using a counter: tlv_buffer[i]. – cdcdcd Nov 28 '16 at 09:42
  • append_bertlv_data() it returns only numbers , for success case, it returns 0, for failure, returning 1 or 10 etc. In this case , it returns 0 which is working fine. i only do not understand tlv_buffer result – Dulguuntur Nov 28 '16 at 09:51
  • Is it the issue that you're not sure about reallocation? If you want to do something then try writing some reallocation code yourself to do it. If there is no documentation to go along with that function and you don't have the source code you can only guess at what it is doing (if the results are unpredictable). We can only guess too. As '733 has stated we're having to guess what things are without their definitions - they're not part of the C standard. Sorry I can't be of any more use. – cdcdcd Nov 28 '16 at 10:57

1 Answers1

1

You can use the following:

for (int i = 0 ; i < strlen(tlv_buffer); i++)
    printf("%02x ",*(tlv_buffer + i));

It will print each byte in hex.

edit:

use a space to separate and if you want the specific length bytes then specify the length instead of size. best is to use strlen.

Sohil Omer
  • 1,171
  • 7
  • 14