Is the strlen function used this way?
If not what would be the alternative?Code:Insert(char *s, int index)
{
int size_of_string = Size();
int length = strlen(s);
int sum = size_of_string + sum;
}
Printable View
Is the strlen function used this way?
If not what would be the alternative?Code:Insert(char *s, int index)
{
int size_of_string = Size();
int length = strlen(s);
int sum = size_of_string + sum;
}
strlen takes the length of a char* string. I don't know what you're trying to do with your code. char* strings are passed by pointer when they're given as arguments. (No extra copy is made, so any modifications made to that string are still there when that function exits.)
Info about strlen e.i. arguments and return values.
Try and print the variable sum. Not as expected? Why? Variable sum contains a garbage value. If this was a pointer variable you would have a serious BUG.Code:Insert(char *s, int index)
{
int size_of_string = Size();
int length = strlen(s);
int sum = size_of_string + sum;//This line is dangerous because sum is uninitialized
}
Solution: Always initialize a variable when it is possible!