How to clear input buffer in C?

The program will not work properly because at Line 1, when the user presses Enter, it will leave in the input buffer 2 character: Enter key (ASCII code 13) and \n (ASCII code 10). Therefore, at Line 2, it will read the \n and will not wait for the user to enter a character. The … Read more

getline() vs. fgets(): Control memory allocation

My question is: Isn’t that dangerous? What if by accident or malicious intent someone creates a 100GB file with no ‘\n’ byte in it – won’t that make my getline() call allocate an insane amount of memory? Yes, what you describe is a plausible risk. However, if the program requires loading an entire line into … Read more

The difference between char * and char[] [duplicate]

The most straightforward answer is: The difference here is that will place Hello world in the read-only parts of the memory and making s a pointer to that, making any writing operation on this memory illegal. While doing: puts the literal string in read-only memory and copies the string to newly allocated memory on the … Read more

Dereference void pointer

printf(“\nlVptr[60 ] is %d \n”, *(int*)lVptr); This will cast the void pointer to a pointer to an int and then dereference it correctly. If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0]. Using [1] is out of bounds, and therefore not a good idea (as … Read more

How to read from input until newline is found using scanf()?

scanf (and cousins) have one slightly strange characteristic: white space in (most placed in) the format string matches an arbitrary amount of white space in the input. As it happens, at least in the default “C” locale, a new-line is classified as white space. This means the trailing ‘\n’ is trying to match not only … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)