I am a bit confused with the next statment in implementation:
void add(char *prefix[NPREF], char *suffix) {
State *sp;
sp = lookup(prefix, 1);
//addsuffix(sp, suffix);
printf("size of prefix %d",&prefix);
printf("size of prefix %s", prefix + 1);
printf("size of prefix %d \n", &prefix+1);
for (int i = 0; i < NPREF; i++)
printf("%s \n" , prefix[i]);
printf("memmove \n");
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = suffix;
for (int i = 0; i < NPREF; i++)
printf("%s \n", prefix[i]);
}
mmemove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
So prefix is a pointer of type char. In this terms prefix + 1 is the link to the next char in the array, isn't it?
How is it works in the right way? I've read about memmove and read about pointers but couldn't explore this add function behaviore for myself.
So prefix is a pointer of type
char
. In this termsprefix + 1
is the link to the nextchar
in the array, isn't it?
No and yes. prefix
is not a pointer to char
, it is an array of NPREF
pointers to char
. The size of this array is
assert(sizeof(prefix) == NPREF * sizeof(char*)
and yes prefix + 1
is address of next element after &prefix[0]
because prefix
(which is array) decays to pointer to array's first element.
So
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
will mommove
NPREF - 1
elements by one location. Sounds reasonable taking into account what Markov chain is, though we can't see your implementation. If it was instead written as
memmove(&prefix, &prefix + 1, sizeof(prefix)/sizeof(prefix[0]);
then it would memmove
whole array.
Example:
[0][1][2][3][4][5]...[n] /* prefix was */
[A][B][C][D][E][F]...[N]
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
[0][1][2][3][4][5]...[n] /* prefix is */
[A][A][B][C][D][E]...[M]