for loop - What does this iterative function in C do? -
specifically, want walk through algorithm starting input pat = "aab"
, string = "ababbaabaa"
. i'm having trouble figuring out going on inside of nested loop because i'm not proficient in c. function looks this:
int nfind(char * string, char * pat){ int = 0, j = 0, start = 0; // int lasts = strlen(string) - 1; // int lastp = strlen(pat) - 1; // int endmatch = lastp; // (i = 0; endmatch <= lasts; endmatch ++, start ++){ // if (string[endmatch] == pat[lastp]){ (j = 0, = start; j < lastp && string[i] == pat[j]; ++, j++) ; } if (j == lastp) return start; } return -1; }
so when function called, we'll have i=j=start=0
, lasts=9
, lastp=2
, , endmatch=2
.
since endmatch < lasts
, run loop. i'm not sure line if (string[endmatch] == pat[lastp])
means. i'm familiar if
statements i'm not sure if they're asking if endmatch == lastp
or if has character arrays. here appreciated.
maybe problem understanding comes lack of understanding of strings in c.
using []
square brackets operator allows access element @ index. shorthand write *(pointer + index)
. uses pointer arithmetics find address pointer + index * sizeof(subscript type)
, , gives direct (referencial) access element dereferencing pointer.
a string in c represented character array finished null character. pat[lasstp]
char
type @ position lastp
in string pat
.
note:
1 needs note there difference between char array[20];
, char* pointer = __alloca(20);
(or whatever exact way space stack). want point out difference between array type , "decaying" of array type pointer type wherever compiler thinks pointer thing can work with, when array type ill fit.
ref: arrays decaying pointers
, what array decaying?
subtle detail though, problem today more basic.
Comments
Post a Comment