Constant pointer vs Pointer to constant

declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified. While declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified. Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):

Multiple definition of … linker error

Don’t define variables in headers. Put declarations in header and definitions in one of the .c files. In config.h In some .c file: If you put a definition of a global variable in a header file, then this definition will go to every .c file that includes this header, and you will get multiple definition … Read more

How to solve error: expected identifier or ‘(‘

You got nested loop with do/while. Make sure that each start with do end with while. Look like at the end of file, the “while” is not correct. That could be you missing the close ‘}’ before ‘while (rows <= height);’ Correct code could be:

How to clear all the elements of array in C?

Your use of strlen() is wrong, that is reliant on the contents of the buffer being a valid string; it doesn’t clear the entire buffer. Just use memset() with sizeof: Note that sizeof is not a function, so no parentheses are needed (or should be used, in my opinion) for cases like these. If your C library doesn’t include memset(), a plain loop … Read more

How to print a char array in C through printf?

The code posted is incorrect: a_static and b_static should be defined as arrays. There are two ways to correct the code: you can add null terminators to make these arrays proper C strings: Alternately, printf can print the contents of an array that is not null terminated using the precision field: The precision given after the . specifies the maximum number of characters … Read more

When and why to use malloc?

malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example when you don’t know the amount of memory during compile time. One example should clear this. Say you know there will be maximum 20 students. So you can create an array with … Read more

How to use glOrtho() in OpenGL?

Have a look at this picture: Graphical Projections  The glOrtho command produces an “Oblique” projection that you see in the bottom row. No matter how far away vertexes are in the z direction, they will not recede into the distance. I use glOrtho every time I need to do 2D graphics in OpenGL (such as health bars, menus … Read more

Going through a text file line by line in C

So many problems in so few lines. I probably forget some: argv[0] is the program name, not the first argument; if you want to read in a variable, you have to allocate its memory one never loops on feof, one loops on an IO function until it fails, feof then serves to determinate the reason … Read more