- Posts: 241
- Thank you received: 14
thomas wrote: Yes, that's a needed fix for NULL from fread_string().
DESCRIPTION
The strchr() function locates the first occurrence of the character c
(converted to a char) in the string s. The terminating NUL character is
considered part of the string. If c is ‘\0’, strchr() locates the
terminating ‘\0’.
The index() function is an old synonym for strchr().
RETURN VALUES
The strchr() function returns a pointer to the located character or NULL
if the character does not appear in the string.
thomas wrote: And, in case someone needs to search for it, { } is called a scope.
It does something - it limits the scope of any variables to this part of the code. In C, you can only define new variables at the start of a scope. So this scope is here to allow us to create the end variable.
thomas wrote: It also means that this is a bit hacky. Had I made code like it at work, my colleagues might have stopped me and instead insisted on making me create a function for this scope. I'll see if I can have a look at improving this later.
Please Log in or Create an account to join the conversation.
It does something - it limits the scope of any variables to this part of the code. In C, you can only define new variables at the start of a scope. So this scope is here to allow us to create the end variable.
In C, you can only define new variables at the start of a scope.
Please Log in or Create an account to join the conversation.
3 bytes*end is mallocated that is 3 bits bigger than the original
There is no condition on the braces, so yes the code is executed, if we enter theSo this tells me that if the preceding code in that case statement is executed then the contents between the braces must be executed.
Which is why new_descr->description must be freed and reassigned to *end as the life of *end as a variable only exists within the scope defined by that set of braces, but new_descr->description is a variable exits in an external scope?
Please Log in or Create an account to join the conversation.
If i remove your {} scope, it compile just fine. Shouldn't 'end' have a scope until break which seems fine for this piece of code, or is it non-standard 'C' doing so? Also, 'end' become permanent memory with CREATE, so this seems all good.
If strchr() finds "\0" in new_descr->description then *end is assigned the address of "\0", if it exists, otherwise *end is assigned "NULL" and the descr->description exits unmodified. But if "\0" is found at the end of the string in descr->description, and there is no "\n" then a new *end is mallocated that is 3 bits bigger than the original descr->description. The contents of descr->description are dumped into *end and "\r\n" are tacked onto the end of *end.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Embarrassingly for me, I actually knew that. Oops.Castillo wrote: 3 bytes
(bits are for binary 01010010
Further embarrassment. I actually do know that strings in C all contain the null character. My apologies for not thinking that part through more carefully.thomas wrote: No, what is actually happening is that we're reusing the end variable.
First off - you need to understand that a string in C is "just a byte array". There's nothing fancy about it [3]. It contains chars by their ASCII byte value. To signify when the string ends, we put in a 0 byte. So the string "hi" will be represented by the char array [104, 105, 0]. The character notation in C uses single quotes and backslashes to signify the same thing: "hi" = 'h','i','\0' = '\104','\105','\0'
So all non-NULL strings contain at least one character - the 0 byte. Using the same annotation, the empty string ("") looks like this : '\0'.
Code:} } // break out of scope. The "end" variable no longer exists.
Castillo wrote: The bigger and new string created by CREATE(end, char, strlen(new_descr->description)+3);
is also permanent memory.
so, new_descr->description will point to a permanent memory string.
It's doesn't matter after that if the variable 'end' is destroyed.
new_descr->description knows that address of the string memory. And, that isn't destroy, neither the permanent memory.
thomas wrote: [1] Reusing is also hacky. As you a evidence of,
Please Log in or Create an account to join the conversation.
tbaMUD © 2024