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!

ckoala
  • 293
  • 4
  • 12
  • 3
    Do you know how to get the first character in a non const string? Have you tried that? – Kevin Nov 26 '20 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? – Eugene Sh. Nov 26 '20 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. – ckoala Nov 26 '20 at 22:14
  • `pointer` would be a *pointer* to the first character. But you already have that: `string` is a pointer to the first character. – Kevin Nov 26 '20 at 22:16
  • I though since neither the pointer nor the string is changeable, I need another pointer to point to the first character of `string`. Basically I want to store a single character in a variable in function scope, then allocate an empty array of the same length, change the stored character and write it to the array. – ckoala Nov 26 '20 at 22:20
  • 1
    You need to show what you are trying to do. Even if it does not compile, it will give us the rough idea. – Eugene Sh. Nov 26 '20 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](https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters) and [Const correctness for value parameters](https://stackoverflow.com/questions/1724051/const-correctness-for-value-parameters). – dxiv Nov 27 '20 at 01:05

2 Answers2

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.

T W Bennet
  • 383
  • 1
  • 7
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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313