How to generate a random number between 0 and 1?

In your version rand() % 10000 will yield an integer between 0 and 9999. Since RAND_MAX may be as little as 32767, and since this is not exactly divisible by 10000 and not large relative to 10000, there will be significant bias in the ‘randomness’ of the result, moreover, the maximum value will be 0.9999, not 1.0, and you have … Read more

Categories C Tags

How to iterate over a string in C?

You want: sizeof gives you the size of the pointer, not the string. However, it would have worked if you had declared the pointer as an array: but if you pass the array to function, that too will decay to a pointer. For strings it’s best to always use strlen. And note what others have … Read more

How to do scanf for single char in C

The %c conversion specifier won’t automatically skip any leading whitespace, so if there’s a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately. One way around the problem is to put a blank space before the conversion specifier in the format string: The blank in the format string tells scanf to … Read more

Anyone know how to solve the error of “collect2.exe: error: ld returned 1 exit status” when a program in C is running?

Overall, your problem is that you’re trying to run a program that is interactive from within Sublime; it doesn’t support that. Sublime captures output your program sends to stdout and stderr and displays it in the output panel, but it does’t connect the panel to stdin. So, the scenario you’re encountering works like this: You run your program, which is … Read more

Why I do get “Cannot find bound of current function” when I overwrite the ret address of a vulnerable program?

The debugger has knowledge about where the code for functions in your program begin and end, either because this information is provided in debugging data or because it uses any external symbols visible in the executable to provide rudimentary information. When the stack is in a proper state, it contains a return address to the … Read more

2D array vs array of arrays

A 2 dimensional array is by definition an array of arrays. What Dave was saying is that in that context, there are different semantics between the definition of a 2D array like this: this: or this:

Is the sizeof(some pointer) always equal to four?

he guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *). In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there’s nothing to be gained in … Read more

How to initialize array to 0 in C?

Global variables and static variables are automatically initialized to zero. If you have simply at global scope it will be all zeros at runtime. But actually there is a shorthand syntax if you had a local array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. You … Read more

Warning: return from incompatible pointer type in C

Well, yes? The function is declared to return char *, but you return i1 which is the input argument and has type int *. You might mean to return the newly allocated string dum, and perhaps also fill it with the data that was read using fgets() to the separate character array input. In this case, you need to copy the data over, … Read more