Difference between sizeof(char) and sizeof(char *)

char is a character and sizeof(char) is defined to be 1. (N1570 6.5.3.4 The sizeof and _Alignof operators, paragraph 4) char* is a pointer to a character and sizeof(char*) depends on the environment. It is typically 4 in 32-bit environment and 8 in 64-bit environment. In typical environment where sizeof(char*) > sizeof(char), malloc(sizeof(char*)*len + 1) will (at least try to) allocate more memory than malloc(sizeof(char)*len + 1) if len is small … Read more

Regular expressions in C: examples?

Regular expressions actually aren’t part of ANSI C. It sounds like you might be talking about the POSIX regular expression library, which comes with most (all?) *nixes. Here’s an example of using POSIX regexes in C (based on this): Alternatively, you may want to check out PCRE, a library for Perl-compatible regular expressions in C. The Perl … Read more

What happens during a “relocation has invalid symbol index” error?

Here is a test reproducing the problem: This produces the following error on GCC 4.8.4: Note that on GCC 6.2.0 the errors related to this question disappear, and it instead produces just: This has been reported a number of times by a number of users, on Stack Overflow, and elsewhere. I would like to understand … Read more

Categories C Tags ,

Why use pointers?

I know this is a really basic question, but I’ve just started with some basic C++ programming after coding a few projects with high-level languages. Basically I have three questions: Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays?

Char Comparison in C

I’m trying to compare two chars to see if one is greater than the other. To see if they were equal, I used strcmp. Is there anything similar to strcmp that I can use?

“Multiple definition”, “first defined here” errors

The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error. The content of commands.h … Read more

double free or corruption (!prev) error in c program

You’re overstepping the array. Either change <= to < or alloc SIZE + 1 elements Your malloc is wrong, you’ll want sizeof(double) instead of sizeof(double *) As ouah comments, although not directly linked to your corruption problem, you’re using *(ptr+tcount) without initializing it Just as a style note, you might want to use ptr[tcount] instead … Read more

Implementing a HashMap in C

Well if you know the basics behind them, it shouldn’t be too hard. Generally you create an array called “buckets” that contain the key and value, with an optional pointer to create a linked list. When you access the hash table with a key, you process the key with a custom hash function which will … Read more

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