On Sep 27, 2002 at 07:40, Nikhil Joshi wrote:
thx guys 4 the suggestions. i've added the following code : But still i get the same error (Segmentation Fault) Also curious: How come the program runs perfectly in DOS without allocating memory ?
DOS ... I'm not sure, but I bet it has something to do with the real memory model. Either that or the Turbo C (what're you using?) compiler does the mallocing.
/* Now Allocate memory for arg[4] I dunno why the following doesn't work: if ((arg = (char *) malloc (30)) == NULL)
See your declaration, you have
char *arg[4];
Now I'm not quite sure if this is an array of pointers (I think it is), or a pointer to an array, but you have to probably do:
for(i=0;i<4;i++) { if ((arg[i] = (char *) malloc (30)) == NULL) perror("malloc"); }
Compiler gives the error: incompatible types in assignment
Naturally, you are assigning char * (the cast) to char ** (the array).
Is there some other method to allocate memory to array of pointers?
Yes.
*/ if ((*arg = (char *) malloc (30)) == NULL)
And here you're mallocing 30 bytes (you should use sizeof(char) to be completely safe, but perhaps ANSI C says that a char is always 1 byte) to arg[][].
I know this is not the right place to discuss C programming but guys pls bear with me ...
It's ok. Once you understand malloc() you'll understand pointers perfectly. I know I didn't get pointers until I understood malloc(). Actually, I grokked both at the same time. I'm now writing a program involving several structs, a call to select, a couple of calls to gettimeofday() (see the manpage), and at least 2 linked lists ::counts: msgid... the packet queue...:: yep, 2.
Okay, so it's not complicated. But have n such programs fork()ed off anotyher one, then initialised from the original based on a text .init file, and then have them do P2P networking, all in 2 weeks....
Yes, I've got C programming on the brain now.
While we're at it, do sendto() and recvfrom() send and receive a full packet, assuming it's SOCK_DGRAM (UDP)?