*** EDIT - I know it is a long post and I know I have used some 'interesting' language choices. And, I'm choosing to believe the three answers I've had so far were made with good intent - for that I thank you. But, I really must insist, if you are not willing to read the whole post thoroughly or if you don't understand what it is I am actually asking, please save both our time and refrain from posting. ***
I have been searching for an answer to exactly the same question posed here - same online course, same lecturer; I recognise the code from the video.
I quickly found solutions in the form of #define and using an enum, but that still didn't really solve how the lecturer got his code to compile; and, I would like to actually comprehend, not just make it work so, I tried to dig deeper.
The professor in question doesn't ever actually show the code compilation, it's always a "here's one I compiled earlier" scenario - so I had included the possibility the error was in that unseen stage as indicated by the two answers on the page: he may well have been using g++ or clang.
However, my search was most circuitous, leading me hither and tither across the net, and in my quest, before I came across the above linked answers and amongst other things I came across this video where at around the 9 minute mark he compiles this code:
#include <stdio.h>
#include <stdlib.h>
const int ARRAY_LENGTH = 5;
void printarray(int myarray[ARRAY_LENGTH]) {
for (int i = 0; i < ARRAY_LENGTH; i++){
printf("%i, ", myarray[i]);
}
printf("\n");
}
int main (int argc, char **argv){
int a[ARRAY_LENGTH] = {1, 2, 3, 4, 5};
int b[ARRAY_LENGTH] = {5, 4, 3, 2, 1};
int *p = malloc(sizeof(int) * 567);
printarray(a);
printarray(b);
printarray(p);
free(p);
}
He uses a makefile that runs gcc with the -g -Wall flags and is on a mac (I think, please correct me if I am wrong). The internet tells me clang is the default compiler on macs, so maybe this is something to do with it (although I have no idea why that would be), but trying this code with g++ gives error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
.
I would just like to understand what is actually going on. Not that consts in C aren't actually constant, more like read-only; use #define/enum. I've read that (probably on here), I've understood it, I've re-written my code and successfully compiled using both methodologies. I've even compiled using const by separating the declaration and assignment. And, at this point, it's not about how the original lecturer compiled his code anymore - I've tested g++ it definitely works, and, if I'd have found that answer earlier I would probably be happily satisfied now with no further questions.
But, I didn't. And, none of what I've found explains how the code from the video compiled and ran (or, why multiple people think this is good practice C coding that should be shared as educational material, but that seems a secondary digression). Maybe its a simple typo, but I've checked and re-checked and I simply cannot find it. Hopefully, someone will be able to adequately put an end to the mystery that plagues me.
Thank you for your time in reading, it seems a bit long but I was trying to be thorough and avoid the all too common initial question-answer ping-pong. No doubt I have still neglected something of the utmost import - c'est la vie.