I'm wondering what this syntax of
strcpy() does in line 65 and 66:
24 #define SEQX "TTCATA"
25 #define SEQY "TGCTCGTA"
61 M = strlen(SEQX);
62 N = strlen(SEQY);
63 x = malloc(sizeof(char) * (M+2)); /* +2: leading blank, and trailing \0 */
64 y = malloc(sizeof(char) * (N+2));
65 strcpy(x+1, SEQX); /* x_1..x_M now defined; x_0 undefined */
66 strcpy(y+1, SEQY); /* y_1..y_N now defined; y_0 undefined */
I know it's copying
y but I don't understand what does the
+1 do? What's the formal name of this type of operation?
Best How To :
pointer + offset notation is used as a convenient means to reference memory locations.
In your case, the
pointer is provided by
malloc() after allocating sufficient heap memory, and represents an array of
M + 2 elements of type
char, thus the notation as used in your code represents the following address:
<x + 1 * sizeof(char)>
And this also happens to be the same as:
In other words, the address of
x (second element of
x). After the
strcpy() operation the memory would look like this:
       
??? 'T' 'T' 'C' 'A' 'T' 'A' '\0'
In other words:
strcmp(x + 1, SEQX) == 0
Note that before practical use of
x as a string, the first memory location should be defined, i.e.
x = '='; // now the string holds "=TTCATA"