A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
C/C++ is a low level language. This means you pass pointers. The & operator is used to pass the address of the pointer, rather than the pointer value. When used with value types like int and float the value is passed on the stack. If you use the & operator, then a pointer to the value is passed.
in Fortran terms value types are passed by value, and the & operator means it’s a pass by reference (default for Fortran).
For glutInit the function is defined as follows
void glutInit(int *argcp, char **argv);
- The first parameter is a pointer to int (pass by ref). it's typically a pointer to main(int argc). not sure why the api picked a pointer. It should be the count of entries in the argv array.
- The second parameter is a pointer to a char pointer. the argv means vector so, its an array. It could be better written as
char *args[], which is a pointer to array of char pointer (strings). This is because strings are defined aschar*, that is a pointer to a character.
in C arrays are passed by reference (same as objects). as arrays don't have any meta data (size), an array variable is a pointer to the first location of the array. the array datatype gives the size of each element. so a variable that is an array of ints is int myarray[] (because it an array its a pointer) or can be specified as int *myarray which is a pointer to an int.
#include <stdio.h>
int main()
{
//define an array of ints
int myArray[] = {0,1,2,3,4,5};
int myArraySize = sizeof(myArray) / sizeof(myArray[0]); // must be computed at compile time
// standard for loop of array
for (int i=0; i < myArraySize; ++i) {
printf("%d\n", myArray[i]);
}
printf("---------\n");
// for pointer loop
for (
int *p1 = myArray; // pointer to array
p1 < &myArray[myArraySize]; // pointer to last element
++p1 // inc pointer
) {
printf("%d\n", *p1);
}
printf("---------\n");
// more common while loop
int *p1 = myArray; // pointer to array
int *p2 = &myArray[myArraySize]; // pointer to last element + 1 (zero based array)
while (p1 < p2) {
printf("%d\n", *p1++);
}
}
so you will need to understand address and pointer. you will see p->myprop to access the property of a class. the myprop is just an offset off pointer p.