0

I have the following code written in C:

unsigned char * pan_protocol_get_image(unsigned long *psize)
{
    long fsize;
    unsigned char *result;

    (void)pan_socket_write_ulong(MSG_GET_IMAGE);
    pan_protocol_expect(MSG_IMAGE);

    /* Read the size of the data in the message */
    (void)pan_read_long(&fsize);
    if (psize) *psize = fsize;

    /*
     * Allocate a buffer large enough for the result. We add one
     * in case the size is zero because we need a valid pointer.
     */
    result = (unsigned char *)malloc(fsize + 1);

    /* Read the data directly into the result buffer */
    (void)pan_read((void *)result, fsize);
    return result;
}

As far as I understand, the function above returns a pointer to a character (and not to an array of characters). Am I correct?

IF that is the case, how can I turn the function result (i.e. a pointer to a character) into an array of characters so that I can read them one by one?

3
  • 1
    Technically, it is a pointer to character. To be precise, it points to a memory location where a character is held. However, nothing prevents that character to be followed by some other characters - which would make sense, because the chunk of memory allocated with malloc seems larger than 1. You can probably effectively reach an array of characters through that pointer. Commented Jul 18, 2013 at 11:10
  • The code above is actually pointing to a character stream/array and not a single character. so you can read the result returned as character array. Commented Jul 18, 2013 at 11:14
  • Given it does malloc(fsize + 1);, I wonder if the intention was to add a trailing \0 Commented Jul 18, 2013 at 11:15

3 Answers 3

2

In C, arrays are just a sequence of elements of the same type. Strings are just arrays, just a sequence of characters.

Arrays (and strings) are represented in only one way - the pointer to the first element (character). As you might conclude, yes, there is no way of knowing the length of the array or the string just by having the pointer to the first element. For this reason, all C strings are appended with a null character '\0' which is just a byte with all zeroes, to detect end of the string. For arrays, you have to pass the length (except some special gotchas, which I'll skip here).

In your program, The malloc() function allocates fsize+1 bytes and returns a pointer to the first byte, which is then returned by pan_protocol_get_image()

As a C programmer, it becomes your responsibility to make sure that the pointer returned from the function points to the first character of the string, and the string has a null terminator.

Just use the returned value as a normal string, it would be fine. Also be wary that you don't know the length of the buffer (string) which is returned by this function.

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

Comments

1

You no need to change your return type. You can directly dereference your memory area allocated by malloc() by using index. You are returning base address of the array. So by adding index to that address you can directly access your allocated memory. You can access like this,

result[0],result[1],....

1 Comment

thanks! great to know sometimes things turn out to be easier than I think
0

psize gets set by the function, if it's not sent a NULL, here

if (psize) *psize = fsize;

So, from outside you should be able to do this:

for (unsigned long i = 0; i<psize; ++i) {
    //do something with result[i] }
}

Don't forget to free the result when you're done with it.

3 Comments

thank you all for the great explanation of what goes on in that function. I have never used malloc before so I wasn't sure what it all did. thanks again, it really helps
@elrim consider a tick for a helpful answer - go look up malloc and free :-)
I tried to vote all answers as useful but it tells me I need more reputation to do that :( so unfair

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.