PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Re: [Cs223] Char Arrays
Date: Sunday, 12 Apr 2020, 08:47:57


    > Message Posted By: Unknown
    >
    > Because we have to change the actual strings (swap two characters in our
    > strings) in the nine20 puzzle, should we be using char arrays? I'm
    > confused how I would dynamically allocate memory for new strings that I
    > create if I do it this way. Or is there a simpler way of allocating the
    > correct amount of space?

Remember that array notation is syntactic sugar;
everything is really done witth pointers.  For example,
if S is a string (i.e., a pointer to a block of storage
containing a null-terminated sequence of characters) and
I and J are indices, then

  char T = S[I]; S[I] = S[J]; S[J] = T;

swaps its I-th and J-th characters.

strwrs required allocating storage for new strings of
known length.  You might also consider using strdup().

--Stan-
PREV INDEX NEXT