I do not understand how execlp() works in Linux

this prototype: Says that execlp ìs a variable argument function. It takes 2 const char *. The rest of the arguments, if any, are the additional arguments to hand over to program we want to run – also char * – all these are C strings (and the last argument must be a NULL pointer) … Read more

Categories C

How to convert a string to integer in C?

There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it’s not portable): You might also be interested in strtoumax and strtoimax which are standard functions in C99. For example you could say: Anyway, stay away from atoi: The call atoi(str) … Read more

c stack smashing detected

I’ve created a file which prints Hello, world as many times at the user wants to give input. No matter what the number entered it always results in a “stack smash”. Here is the program, can anyone come up with a conclusion to why it is doing this? Here is the “traceback” that occurs after … Read more

1 = false and 0 = true?

It is common for comparison functions to return 0 on “equals”, so that they can also return a negative number for “less than” and a positive number for “greater than”. strcmp() and memcmp() work like this. It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the … Read more

Pointer Arithmetic

First, the binky video may help. It’s a nice video about pointers. For arithmetic, here is an example: (Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements … Read more

How do I determine the size of my array in C?

Executive summary: Full answer: To determine the size of your array in bytes, you can use the sizeof operator: On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array … Read more

Stack smashing detected

Stack Smashing here is actually caused due to a protection mechanism used by gcc to detect buffer overflow errors. For example in the following snippet: The compiler, (in this case gcc) adds protection variables (called canaries) which have known values. An input string of size greater than 10 causes corruption of this variable resulting in … Read more