My confusion starts at pathNode = malloc(sizeof(Node))
and ends atreturn (head);
First , I don't understand why this function stores the size of a type variable called "Node", then stores that information in a variable called pathNode
, and right after that there is one declaración SI
that is executed in case it pathNode
is a value NULL
, I mean, how could that be possible if the variable of type "Node" is fixed (at least initially) ?
Second , I understand that the function strtok
is used to split a string using a delimiter, which in this case is :
. I also know that the arrow operator ->
is used to access elements in Estructuras
.
But then the following lines of code pathNode-> str = token;
and pathNode-> next = head;
are like, some kind of inverted variable declaration , which makes the syntax seem weird to me, and these variables str
and next
weren't declared like in the case of pointers, isn't that necessary ?
And finally , there is the while loop, which I assume will run until it finds the element \0
at the end of the chain pathCopy
. However, there is again the use of pathNode = malloc (sizeof (Node));
, pathNode-> str = token;
and pathNode-> next = head;
, bringing me back to the previous confusion...
The code is the following:
typedef struct Node
{
char *str;
struct Node *next;
} Node;
Node *_getdir(char *path, char **pathCopy)
{
char *token = NULL;
Node *head;
Node *pathNode;
if (path == NULL)
return (NULL);
*pathCopy = strdup(path);
head = NULL;
pathNode = malloc(sizeof(Node));
if (pathNode == NULL)
return (NULL);
token = strtok(*pathCopy, ":");
pathNode->str = token;
pathNode->next = head;
head = pathNode;
while (token != NULL)
{
token = strtok(NULL, ":");
if (token == NULL)
break;
pathNode = malloc(sizeof(Node));
if (pathNode == NULL)
return (NULL);
pathNode->str = token;
pathNode->next = head;
head = pathNode;
}
return (head);
}
path = "/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin"
char *pathCopy = NULL;
pathDirs = _getdir(path, pathCopy);
Let's go with your doubts one by one:
first .
malloc()
takes care of looking for a zone of contiguous bytes that is free in a part of memory called heap . What you pass to it as a parameter is simply how many bytes you want (in this case, the number needed to hold a structure of typeNode
). Ifmalloc()
it finds a free zone of that size, it returns a pointer to it. If it cannot find it (because the whole heap is busy or very fragmented and it is not possible to find that number of contiguous bytes ), it returnsNULL
.So the check for
NULL
is typically done right after amalloc()
, as it may be the case that there is no memory to create a newNode
.Second The "variables" (as you call them)
str
andnext
are actually fields of a structure. The C syntaxpuntero->campo
is the same as(*puntero).campo
. That is, it is expected topuntero
point to a structure and tocampo
be the name of a field in that structure.In your case
pathNode
it is a pointer toNode
. At the beginning of the code you can see how this type is declared:and we see that it does indeed have the fields
str
andnext
.Third . This question is related to how it works
strtok()
, which has a really strange interface. The first time you callstrtok()
you must pass a pointer to a string as the first parameter (and as the second another string that will be used for hashing). Instead, in all subsequent iterations, it must be passedNULL
as the first parameter. That tellsstrtok()
it to continue working with the previous chain and to return the next token (which it had returned the previous time). When there are no more tokens it returnsNULL
.Therefore the while loop is iterating through all the pieces that result from dividing the string it points to
pathCopy
using as divider":"