Tuesday, 9 May 2017

What are the rules for casting pointers in C?

K&R doesn't go over it, but they use it. I tried seeing how it'd work by writing an example program, but it didn't go so well:



#include  
int bleh (int *);


int main(){
char c = '5';
char *d = &c;

bleh((int *)d);
return 0;
}

int bleh(int *n){

printf("%d bleh\n", *n);
return *n;
}


It compiles, but my print statement spits out garbage variables (they're different every time I call the program). Any ideas?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...