Question 4.2

I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code?

char *p;
*p = malloc(10);


The pointer you declared is p, not *p. To make a pointer point somewhere, you just use the name of the pointer:

	p = malloc(10);
It's when you're manipulating the pointed-to memory that you use * as an indirection operator:
	*p = 'H';

See also questions 1.21, 7.1, and 8.3.

References: CT&P Sec. 3.1 p. 28


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback