Why Use Pointers?


Problems

Problem : Why can't you dereference a void pointer?


Problem : Given the function: void print_bit_int(int value); which takes an integer as a parameter and prints out its bit representation, write a line of code which prints out the bit representation for a float spark (you can assume that a float is the same size as an integer).


Problem : Write a function, memcmp(), which takes two void pointers and a length in bytes, and compares the memory at those two locations for that many bytes. It should return a non-zero value if the memory matches, and zero if the memory does not match.


Problem : What is wrong with the following code? How would you fix it with a cast?


int main()
{
	int steve;
	int *spark;
	void *notes;

	steve = 500;
	spark = &steve;
	notes = (void*)spark;

	*notes = 600;
	printf("%d\n", steve);
	return 0;
}


Problem : What is wrong with the following code?


int main()
{
	int a, b;
	double d, e 
	void* v[10];
	v[0] = &a;
	v[1] = &d;
	v[2] = &b;
	v[3] = &e;

	int x = *((int*)v[0]);
	double w = *((double*)v[1]); 
	int y = *((int*)v[1]);
	
	return 0;
}