Returning an array using C

You can’t return arrays from functions in C. You also can’t (shouldn’t) do this: returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns. You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer … Read more

What is the difference between %f and %lf in C?

The short answer is that it has no impact on printf, and denotes use of float or double in scanf. For printf, arguments of type float are promoted to double so both %f and %lf are used for double. For scanf, you should use %f for float and %lf for double. More detail for the language lawyers among us below: There is no difference between %f and %lf in the printf family. The ISO C standard (all references within are from C11), section 7.21.6.1 The fprintf … Read more

Categories c Tags

Warning/error “function declaration isn’t a prototype”

In C int foo() and int foo(void) are different functions. int foo() accepts an arbitrary number of arguments, while int foo(void) accepts 0 arguments. In C++ they mean the same thing. I suggest that you use void consistently when you mean no arguments. If you have a variable a, extern int a; is a way to tell the compiler that a is a symbol that might be present in a … Read more

Categories c Tags ,

Bind failed: Address already in use

The error usually means that the port you are trying to open is being already used by another application. Try using netstat to see which ports are open and then use an available port. Also check if you are binding to the right ip address (I am assuming it would be localhost)

How to use S_ISREG() and S_ISDIR() POSIX Macros?

You’re using S_ISREG() and S_ISDIR() correctly, you’re just using them on the wrong thing. In your while((dit = readdir(dip)) != NULL) loop in main, you’re calling stat on currentPath over and over again without changing currentPath: Shouldn’t you be appending a slash and dit->d_name to currentPath to get the full path to the file that you want to stat? Methinks that similar changes to your other stat calls are also needed.

Categories c

Convert char array to a int number in C

I personally don’t like atoi function. I would suggest sscanf: It’s very standard, it’s in the stdio.h library šŸ™‚ And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end. EDIT I just found this wonderful question here on the site that explains and compares 3 different ways … Read more

C Vector/ArrayList/LinkedList

For resizable arrays you can use malloc() and realloc(). These allow you to reserve (with malloc()) and resize (with realloc()) a certain amount of space on the heap. They’re used this way: Just replace ‘int’ with your struct type. šŸ˜‰

Categories c Tags

Setting std=c99 flag in GCC

Instead of callingĀ /usr/bin/gcc, useĀ /usr/bin/c99. This is the Single-Unix-approved way of invoking a C99 compiler. On an Ubuntu system, this points to a script which invokesĀ gccĀ after having added theĀ -std=c99Ā flag, which is precisely what you want.

expected expression before ‘{‘ token

The error is because you can’t assign an array that way, that only works to initialize it. So applying this to your specific example: or you could use memset the same way, but instead of sizeof(int) you need size of your structure. That doesn’t always work… but given your structure, it will.

difference between and

The <malloc.h> header is deprecated (and quite Linux specific, on which it defines non-standard functions like mallinfo(3)). Use <stdlib.h> instead if you simply need malloc(3) and related standard functions (e.g. free, calloc, realloc ….). Notice that <stdlib.h> is defined by C89 (and later) standards, but not <malloc.h> Look into /usr/include/malloc.h you’ll find there some non-standard functions (e.g. malloc_stats(3), etc…) – in addition of malloc…. And gcc don’t link header files, but libraries. Read Levine’s book about linkers & loaders for more. … Read more

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