Pointer to a string in C?

The same notation is used for pointing at a single character or the first character of a null-terminated string: The values in ptr2 and ptr3 are the same; so are the values in ptr4 and ptr5. If you’re going to treat some data as a string, it is important to make sure it is null terminated, and that you know how much … Read more

Initializing 2D char array in C

I think pictures help. Here is char newarray[5][10]. It is a single memory block consisting of an array of 10 characters, and an array of five of those. You could just clear it with a single memset call. Here is char **array. It says array is a pointer. What is it a pointer to? a pointer to a character. Keep in … Read more

Can I get Unix’s pthread.h to compile in Windows?

pthread.h is a header for the Unix/Linux (POSIX) API for threads. A POSIX layer such as Cygwin would probably compile an app with #include <pthreads.h>. The native Windows threading API is exposed via #include <windows.h> and it works slightly differently to Linux’s threading. Still, there’s a replacement “glue” library maintained at http://sourceware.org/pthreads-win32/ ; note that it has some slight incompatibilities with … Read more

How to free memory from char array in C

Local variables are automatically freed when the function ends, you don’t need to free them by yourself. You only free dynamically allocated memory (e.g using malloc) as it’s allocated on the heap: More about dynamic memory allocation: http://en.wikipedia.org/wiki/C_dynamic_memory_allocation

Printf was not declared in this scope

The compiler didn’t find declaration for printf function. That’s why it shows compilation error. The correct declaration (ISO/IEC 9899:1999) of printf function is: You can either declare the function like above before calling it or you can include header file which contains declaration of that function. But it would be easiest and safest to just include the header file which contains … Read more

Split string with multiple delimiters using strtok in C

A simple example that shows how to use multiple delimiters and potential improvements in your code. See embedded comments for explanation. Be warned about the general shortcomings of strtok() (from manual): These functions modify their first argument. These functions cannot be used on constant strings. The identity of the delimiting byte is lost. The strtok() function uses a static … Read more

How to know what the ‘errno’ means?

You can use strerror() to get a human-readable string for the error number. This is the same string printed by perror() but it’s useful if you’re formatting the error message for something other than standard error output. For example: Linux also supports the explicitly-threadsafe variant strerror_r().

Pointer to 2D arrays in C

Using pointer2 or pointer3 produce the same binary except manipulations as ++pointer2 as pointed out by WhozCraig. I recommend using typedef (producing same binary code as above pointer3) Note: Since C++11, you can also use keyword using instead of typedef in your example: Note: If the array tab1 is used within a function body => this array will be placed within the call stack memory. But the stack size is limited. Using … Read more

write() to stdout and printf output not interleaved?

Printf is buffered. You can force printf to ‘flush’ its buffer using the fflush call: In general, printf() being buffered is a good thing. Unbuffered output, particularly to visible consoles that require screen updates and such, is slow. Slow enough that an application that is printf’ing a lot can be directly slowed down by it (especially on … Read more