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(constchar *);
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.
int strlen(constchar * 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
int strlen(constchar * 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 :)