[Beds] Programming

Adrian St. John adrian at stjohn-home.net
Fri Jan 27 22:03:55 GMT 2006


On Fri, Jan 27, 2006 at 07:00:26PM -0000, Stephen Elliott wrote:
> Hi,
> 
> I have a C program here which a friend has sent me and I am trying to
> understand. I did basic C programming when I was at college and wrote quite
> a nice Shipbourne radar system, but this one blows me.
> 
> It uses pointers and I understand the principle behind them. I was wondering
> if someone could explain the code to me, the line I am having the most
> trouble with is the calloc line.

I can give you some information about that line.

char *c; int fd, sz; c = (char *) calloc(100, sizeof(char));

Ok, the thing to remember is that every ; is the end of a statement, so
this is actually 3 statements:

(1)	char *c;
(2)	int fd, sz;
(3)	c = (char *) calloc(100, sizeof(char));

Statement 1 is declaring that c is a pointer to a character (typically used
for strings)

Statement 2 is declaring that fd and sz and ints (32 bit signed integers)

Statement 3: Call the calloc function. The parameters to calloc are amount
(100) and size (sizeof(char)). This is allocating memory for 100
characters, and setting them all to 0.

It returns the address of the memory. the (char *) before it is
converting the address from a generic pointer (void *) to char *.

I was going to suggest man calloc, but I just had a look and realised
its not the easiest man page to understand.

I hope this helps.

> 
> Cheers
> Steve...

Adrian




More information about the Beds mailing list