1
const char* occurs(const char* s1, const char* st){
  if(length(st)>length(s1)){
    return 0;
  }

  int diff=length(s1)-length(st);
  int i=0, j=0, k=0;

  for(;i<=diff;i++){
    j=i;

    while(s1[j]==st[k] && st[k]!='\0'){
      k++;
      j++;
    }

    if(st[k]=='\0') return &s1[i];
  }
    return 0;
}

This function returns the first occurrence of a string in another string. In my main function, I call it like this:

const char* occ = occurs(argv[1], argv[2]);
std::cout << &occ << " - " << occ << std::endl;

What confuses me is: being occ a pointer, shouldn't I get the address by writing the variable as is (occ) and the value by dereferencing it (*occ)? Why does it seem to be working like a normal variable (Getting the address with &occ and the value with occ)? Does C++ change something about a pointer if I treat it like an array? If I solved the problem by considering s1 and st as pointers, without using the array semantic, would it have been the same in regards to the memory?

3
  • 1
    The output operator << has a special overload for const char* to print a c-style string. Commented Oct 28, 2017 at 13:29
  • calling length again and again is not a good idea. Better use str::string Commented Oct 28, 2017 at 14:09
  • I see, didn't know about <<. Also, I know it's not a good idea, it's just an exercise to understand better pointers/arrays (so not using str::string) and I didn't bother saving the lengths in different variables, ahah. Commented Oct 28, 2017 at 14:21

2 Answers 2

2

You forget that C-style strings (null-terminated byte strings as they are really called) are handled by a special overload of the << operator to print the string itself.

What you print with &occ is the address of the occ variable itself, not where it points.

If you want to print the value contained inside the occ variable (i.e. where it points) then you need to cast it to a pointer-type which isn't handled specially like strings. Like e.g. static_cast<const void*>(occ).

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

3 Comments

So, &occ is the address of the cell where the pointer to the C-style string is stored? If occ wasn't a C-style string, but a pointer to an array (or a pointer to something) I would've had to write std::cout << occ << " - " << *occ << std::endl; to actually see address - value, is that correct?
@Dodicin A string is really nothing more than an array of characters, with a special character terminating it. So as long as you don't have a string (if e.g. occ was an int*) then yes that's correct. Also remember that if occ points to the first element of an array then *occ will give you the value of that first element, it's equal to occ[0].
Thanks for the clarification!
0

Yes, c++ did change something. you have set the return type of function "occur" to const char* so this basically returns the pointer to the address of a variable. But in your program you have returned a const r-value "0", what c++ did is that it didn't saw this as an r-value (which would lead to an error for values other than 0) it saw it as if it were a null pointer, that's why it didn't gave an error either. so when you did this:-

const char* occ = occurs(argv[1], argv[2]); "occ" was pointing to an invalid address and that's why didn't work as you would expected it to.

anyway, this was my explanation , feel free to confirm it as it may not answer your question precisely.

1 Comment

I don't think that's it! I know 0 is considered as a null pointer, but the << override explains the unexpected behaviour I wasn't understanding better.

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.