0

I have this function declaration:

void gaussian_blur
(
    const unsigned char* const inputChannel,
    unsigned char* const outputChannel,
    const float* const filter
) 
{..}

I'm new to C. I've gone through basic tutorials and understand the concept of pointers and constants, but I can't make sense what is meant by this argument list.

Can anyone explain what this means?

5
  • 2
    If you understood pointers, where is the problem? Commented Mar 7, 2013 at 17:56
  • What is const unsigned char* const ? Is it immutable variable of type unsigned char* const ? What type is it ? Commented Mar 7, 2013 at 17:57
  • this means, that the pointer ist constant and the target data ist constant as well. Commented Mar 7, 2013 at 17:58
  • Are you confused about the types--as many of the comments assume--or about the intended meaning of the arguments? Or both? Commented Mar 7, 2013 at 18:06
  • About the types. Apparently I did'n understand pointers as good as poitroae does. I was absolutely missing the thing called constant pointer Commented Mar 7, 2013 at 18:11

3 Answers 3

4
const unsigned char* const inputChannel;

Input channel is a constant pointer to a constant unsigned char.

unsigned char* const outputChannel;

OutputChannel is a constant pointer to a unsigned char.

const float* const filter

filter is a constant pointer to a constant float.

Constant pointer - The address held by the pointer variable cannot be changed.

Constant data - The data , is treated as readonly/constant and cannot be modified.

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

1 Comment

@Alex no problem anytime , you will understand better if you try to modify contents of const pointer / const data , and see the warnings/errors the compiler will throw at you
3

Read right to left:

const unsigned char* const inputChannel

inputChannel is a constant pointer to an unsigned char constant.

Comments

2

If you look at the info link of the tag, which you tag this question with, scroll down to

Important notes that may save you time

And you'll see a link to the Spiral rule which is very helpful for deciphering the meanings of things you don't understand in C:

The basics of it are to read the name of the variable, then move your way around right and then left one element at a time. In this case because there's nothing to the right of your variable you just move left:

 inputChannel,                           // inputChannel is a
 const inputChannel,                     // constant
 * const inputChannel,                   // pointer
 char* const inputChannel,               // to a char
 unsigned char* const inputChannel,      // which is unsigned
 const unsigned char* const inputChannel // which is constant

So it's a constant pointer and what it points to (an unsigned char) is also constant. Pretty easy.

If that's too much you can always cheat too, throw what you don't understand into here: http://cdecl.org/ and it spits it back in English (usually)

1 Comment

@Alex - NP, it's a handly link, just keep in mind if you're reading code offline/not at a computer it's important to know how to do it by hand.

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.