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 SEQX
and SEQY
into x
and y
but I don't understand what does the +1
do? What's the formal name of this type of operation?
The
pointer + offset
notation is used as a convenient means to reference memory locations.In your case, the
pointer
is provided bymalloc()
after allocating sufficient heap memory, and represents an array ofM + 2
elements of typechar
, thus the notation as used in your code represents the following address:And this also happens to be the same as:
In other words, the address of
x[1]
(second element ofx
). After thestrcpy()
operation the memory would look like this:In other words:
Note that before practical use of
x
as a string, the first memory location should be defined, i.e.