0

I have a function with the signature

char * processString(const char * const string);

This function is passed a constant pointer to a constant string defined in main()

int main(void) {
  myString[] = "Hello";
  char * ptr = processString(myString);
}

Inside processString() I want to iterate over each character, but I am stuck at grabbing the first character from this const pointer to const string.

I read this somehow related post (among others) c syntax passing const pointer to const data to function

I tried initializing a second pointer to read each character of the string one by one

char * processString(const char * const string) {
  char * pointer = &string;
  return pointer;

Your help is very much appreciated!

7
  • 3
    Do you know how to get the first character in a non const string? Have you tried that? Commented Nov 26, 2020 at 22:11
  • but I am stuck at grabbing the first character - what does it mean? And how the const-ness of the string is related to it? Commented Nov 26, 2020 at 22:12
  • I have tried this inside the function char *pointer = string which should be the first character, I think. Although I am not sure because I didn't fully understand pointers yet. Commented Nov 26, 2020 at 22:14
  • 1
    You need to show what you are trying to do. Even if it does not compile, it will give us the rough idea. Commented Nov 26, 2020 at 22:27
  • 1
    @ckoala Maybe ask first what is the point of making the argument const string. The pointer is passed by value, so the function cannot modify the original pointer, anyway. See for example Use of 'const' for function parameters and Const correctness for value parameters. Commented Nov 27, 2020 at 1:05

2 Answers 2

4

Iterating over the string isn't a big problem

char * processString(const char * const string)
{
    for(const char *scan = string; *scan; ++scan) {
        putchar(*scan);
        putchar('\n');
    }
}

The parameter string can be assigned to scan as shown without problems. That inner const doesn't prevent it because the initialization of scan is just copying the pointer value.

If you want to return scan, that's a problem. You either have to cast it to remove the outer const, which is a bad idea, or perhaps you can change the function return type to const char *. Otherwise, you will need to do some copying.

The assignment you have, char * pointer = &string; fails because the expression on the right has type char ** which doesn't match.

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

Comments

1

If you feel like changing string‘s value, it’s completely valid to declare the function with a const argument, and then define it with a non-const argument (or vice versa). We’re of course talking about the value of the pointer argument – not about the characters it points to: those must remain constant or you’ll get a declaration-definition mismatch error. It’s much better form, through, to declare a loop variable in for and change that, leaving the argument’s value alone. It has no performance implications in release mode: any decent compiler will generate identical code unless aliasing comes into play.

Comments

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.