Since C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
Answer from templatetypedef on Stack OverflowSince C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
If you want to check if a string is empty:
if (str[0] == '\0')
{
// your code here
}
Videos
Yes this
void function(void){}
is possible and it's a correct function definition.
the function can't have arguments and it can return nothing ? But it can have it's own function body though it doesn't expect any argument & return nothing.
For e.g
void function(void){ printf(" welcome to SO\n"); }
Would that be correct?
void function(void) {}
Yes, a function that does not accept any arguments, it does not return anything, and does nothing in its body.
I'm new to C and I'm trying to iterate over an array and when all values up to max size are assigned I have no problem but when I leave any values empty and do the for loop it takes some junk values from memory. I was wondering if there is some "end value" like \0 for string to know where to stop.
Sorry for the bad english and thank you in advance
The error you are getting is caused by the fact that you declared your function as bool isEmpty(LinkedListNode) but you are trying to define it as bool isEmpty(LinkedListNode*). The difference is that in the definition you have a pointer, while in the declaration there is just an object. You have to pick one, as these are completely different things.
That said, I don't see why you need the argument at all to check whether your list is empty. Just drop the argument altogether and use if ( head->next == NULL ) - non-static member functions are always called through an instance of the class.
Just for completeness, first item in your list is pointed to by head, so in order to check if there is anything in the list, you should check if it is a null pointer:
bool IntLinkedList::isEmpty() const
{ //added const for const-correctness, should be added to declaration as well
return head == nullptr;
}
Following list.empty(),
Returns whether the list container is empty (i.e. whether its size is 0).
Two suggestions:
Have a size variable that checks for the number of nodes in your list, this way your isEmpty() is just return size == 0;
Or in your current implementation, just modify to:
bool isEmpty() {
return head == null; // if head is null, there's no node in list
}
Hello! I'm tasked with creating a function that reads a file and returns a string for each line.
Using the automated test, I see that I am allocating memory when I shouldnt, since the file is empty.
Problem is, I can only use Free, Malloc and Read.
I am given the fd as parameter, and can only use said functions and my own.
Is there anyway, using those functions, to check if the file is empty or not, so I can then decide if I need to set space in memory?