-
I dont Know
I dont know where to start.
Write two versions of function strlen in Fig. 8.36. The first version should use array subscripting, and the second version should use pointers and pointer arithmetic.
8.36 says this:
size_t strlen(const char *s);
Determines the length of string s. The number of characters preceding the terminating null character is returned.
-
No. 1 refers to this:
Code:
char string[]="this and that";
int i=0;
while (string[i]!='\0') {
...etc.
the "array subscript" is the [bracketed element number].
No. 2 is this kind of stuff
Code:
char string[]="this and that";
char *ptr=string;
int dif;
while (ptr!='\0') { ptr++; }
dif=ptr-string;
See? Pointer arithmetic. Memory addresses are in bytes, which are equal to one char, so for example the address "&string[1]" will be one integer value higher than "*string" itself.
I am pretty sure this is guaranteed by the compiler. Positive actually.
-
Ok I think I get the 2nd one. The first one tho is where Im confused at. What am I using strlen for? Like what u have, what does that have to do with strlen?
-
I would lie and say that it has nothing to do with strlen, but if I tell the truth and say it is 3/4 of strlen, will you believe "i++" is at least another 1/8th of that algorithm?