Pointer-offset notation??

Feb 6, 2020 at 10:03pm
I got the question that I have to do but I missed the first day of class and have idea what is going on.

a. Define a function strLength() to calculate the number of characters in a C-string and return it. Use pointer-offset notation. Test it with a sample C-string in your main function. Here is the function prototype:

int strLength(const char * );

This probably won't make any sense to anyone but it what I have been trying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int counter;
int strLength(const char *myptr)
{
    while (myptr != '\0')
    {
        counter++;
        myptr++;
    }
    return counter;
}
main(){
string b = "hello";
const char *ptr = &b;
strLength();
    return 0;
}
Feb 6, 2020 at 10:15pm
Couple things...
1. you only ever use counter within your strLength function, so there is absolutely no reason to make it a global variable.
2. the proper syntax is int main() { ... }, not just main().

By "pointer-offset notation", your instructor means dereferencing a pointer with an offset.
For example, if myptr points to a char array (c-string), then myptr[1] is the second character of that string.
myptr[1] is just "syntactic sugar" for *(myptr + 1).

So, instead of + 1 as the offset, you can check the current character by doing *(myptr + counter) instead of incrementing myptr directly.

In the real world, I would never suggest writing *(ptr + offset), because it reduces readability.
Do ptr[offset] instead.
Last edited on Feb 6, 2020 at 10:17pm
Feb 6, 2020 at 11:27pm
#include <iostream>
#include <string>
using namespace std;
int strLength(const char *myptr)
{
int counter = 0;
while (*myptr != '\0')
{
*myptr += counter;
counter++;
}
return counter;
}
int main(){
string b = "hello";
const char *ptr = &b;
return 0;
}

I believe my professor wanted it done without using the array notation stuff ( [] ). Its giving me errors when I try to run this version. Any extra tips would be appreciated
Feb 6, 2020 at 11:35pm
I believe my professor wanted it done without using the array notation stuff ( [] ).
Yes, I figured as much. That's why I said "in the real world" don't do it like that. Don't worry about it for now.

Why don't you post what the errors are? And please use code formatting like you did in your first post.

1. "c-style strings" (char arrays) and std::strings are two different things. You can't directly point a const char* to an std::string's location like that.

2. In your strLength function, you still aren't using the *(ptr + offset) notation anywhere.
Don't try to modify the data myptr points to -- it's const.
Again, instead of modifying myptr, use the "pointer-offset notation"
e.g. *(myptr + counter) != '\0'

3. If you initially have a string, you can do:
const char* ptr = b.c_str();
to get the pointer.
Last edited on Feb 6, 2020 at 11:36pm
Feb 6, 2020 at 11:36pm
> Its giving me errors when I try to run this version.
¿what errors?
¿does your program compile? ¿does it crash? ¿it gives a different answer than what's expected?

right now you are not even calling the function.
Feb 6, 2020 at 11:54pm
Sorry for being such a nub at this. I use Code::Blocks 17.12
This is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int strLength(const char *myptr)
{
    int counter = 0;
    while (*(myptr + counter) != '\0')
    {
        counter++;
    }
    return counter;
}
int main(){
const char b = 'hello';
const char* ptr = &b;
cout << strLength(*ptr);
    return 0;
}


The 16 line gets a red block next to it and this shows up in the build messages:
|16|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
Last edited on Feb 6, 2020 at 11:56pm
Feb 7, 2020 at 1:30am
You're close. Just do const char* b = "hello"; instead.
Notice the double quotes, and that you're declaring a pointer. (It's pointing to the data that is "hello", which is null terminated by the way).
Topic archived. No new replies allowed.