I am writing the following string in the main function:
char* dict[] = { "ball", "aba", "bla" };
My question is:
Am I able to take a whole word out of this string?
For example, if I want to copy the whole word, can I do something like this:
str[j] = dict[i];
(in some loop of course)
It is important to note that I can't use the #include<string.h>
library, due to the requirements of the question
A string in C is "Null terminated". It's actually an array of
char
ended by "0". For example, we have:This statement will create an array of 4 elements,
'a', 'b', 'c', 0
, andstr
is the pointer to the array (or pointer to the first element of the array). And the value ofstr
is just an address, which is just an integer number. If you copy the string in the way ofstr[j] = dict[i]
you copy only the address (shallow copy). It will not duplicate the string.In your case, you create a list of strings (array of char), and
dict[i]
is the pointer to the first element of the i-th string. In other words, we can handledict[i]
like a regular string (e.g.str
in my example).This is an example of creating a deep copy of your list.