Pointers Homework

Pages: 1234
Hello ! I started to learn to work with pointers today. I wanna make some exercices, but I don't know what should they sound.


I write, I experiment things and I like how they work. But I want to test my knowlage with some homework.

Can you provide me some exercices ? :)
1. Write a function that returns the size of the string pointed to by a passed pointer. Assume the pointer is valid. the prototype for the function is
int strlen(const char *);
Remember that strings are null-terminated. Don't count the null character in the returned size.

2. What's wrong with this code?
1
2
char *string="hello";
string[0]='A';


3. What's the value of x at the end of this code? Warning: a compiler's output is not a reliable to confirm the answer.
1
2
3
4
const int a=15;
int *b=(int *)&a;
*b=20;
int x=a;
Last edited on
2. The bug is that string points to something which doesn't exist in memory. It's the same with

int *pointer = 1;

3. x is 15

I'll make the first exercise tonight. Are 2 and 3 right ?
Both answers are wrong.
Warning: a compiler's output is not a reliable to confirm the answer.


Doesn't that, by definition, mean the answer you're thinking of is wrong?

Way to throw curveballs in the beginner's forum XD

EDIT: nevermind -- I know what answer you're looking for now. Haw.
Last edited on
When you want to test corner cases, you don't test under normal conditions.
What is that code all about? And your answers make little sense.
Last edited on
@helios
could you post the correct answer tomorrow? I want to see if my answers are all correct :)
Last edited on
1.
1
2
3
4
5
6
7
int strlen(const char * str)
{
    int strlength = 0;
    while(str[strlength] != '\0')
           strlength++;
    return strlength;
}

3. x = 20

edit: not sure of 2.. still thinking about it but what i could get off the top of my head is, probably a char * acts like a string literal so it is constant? I am still a c++ beginner, not used to those char * yet. :D
Last edited on
1. Can you do it without using the offset operator?

3. Still wrong.
1.
1
2
3
4
5
6
7
int strlen(const char * str)
{
     int strlength = 0;
     while(*(str + strlength) != '\0')
             strlength++;
     return strlength;
}


Is number 2 correct then? =o

3. I really can't find out, first you specify a constant "a" = 15, then you create a pointer to it "b" and then you dereference "b" and change its value which as far as i know changes the value of "a" then you assign "a" to the value of "x", if I am wrong please correct me :)
Last edited on
1.
*(str + strlength)
That's in the definition of "offset operator".

3. The correct answer can be found in these fora.
1. My last shot, I can not think of another function now..
1
2
3
4
5
int strlen(const char * str)
{
int length = strcspn(str, "\0");
return length;
}


3. I will start searching, thanks for giving such an example :)
Last edited on
strcspn
Now, now, don't be silly. If I was to allow that, you may as well return strlen(p);.
Last edited on
Oops i went off the scope..
Well, then I can't.
Consider this:
1
2
3
4
5
int strlen(const char *s){
    char *p=s;
    for (;*p;p++);
    return p-s;
}
Wow, i actually did something like this but it didn't work for some reason, now i found out :D
1
2
3
4
5
6
7
8
9
10
11
int strlen(const char* str)
{
	int len = 0;
	char * szSt = const_cast<char*>(str);
	while(szSt != "\0")
	{
		szSt = (szSt + 1);
		len++;
	}
	return len;
}


It should have been
1
2
3
4
5
6
7
8
9
10
11
int strlen(const char* str)
{
	int len = 0;
	char * szSt = const_cast<char*>(str);
	while(*szSt)
	{
		szSt = (szSt + 1);
		len++;
	}
	return len;
}
Oops. Line 2 above should be const char *, not just char *.
分かりにくいなぁ!
Pages: 1234