Hi,
You got divine hands :-). Though it worked, you should still allocate memory to arg itself for holding array of pointers. Something like
arg = (char **) malloc(sizeof(char *) * 6); arg[5] = NULL;
Amish.
Nikhil Joshi wrote:
Hi!
success afterall ... I used following to allocate memory to arg
for (i=1;i<4;i++) if ((arg[i] = (char *) malloc (30)) == NULL)
On Fri, 27 Sep 2002, Amish Mehta wrote:
You got divine hands :-). Though it worked, you should still allocate memory to arg itself for holding array of pointers. Something like
No he shouldn't. When he does char *arg[4]; That's when memory is allocated for four pointers. If instead he had done char **arg, then he would have to allocate memory himself. Now, he only has to allocate memory for each of the four pointers in the array, using the code that he already has.
Nikhil, I hope this answers your question too about how to allocate memory for an array of pointers.
hi,
delta error amish.. that should b for char ** arg; but nikhil got char *arg[4]; :-) nikhil.. any other methods .. let us c ... char arg1[BUF_SIZE]; char *arg[4] int i; for(i=0; i<4;i++) arg[i] = &arg1[0]; .. /* oops but i m burying all args in to same grave ;-) */ so .. ... char arg1[BUF_SIZE], arg2[BUF_SIZE], arg3[BUF_SIZE], arg4[BUF_SIZE]; arg[0] = &arg1[0]; arg[1] = &arg1[0]; arg[2] = &arg1[0]; arg[3] = &arg1[0]; ... /* ohh.. that could b painful digging if too many to b buried */ ... char arg1[BUF_SIZE * 4] for(i=0; i<4; i++) arg[i] = &arg1[i * BUF_SIZE]; ... /* gosh.. if u bury anybody longer than BUF_SIZE.. u r tampering others grave */ for(i=0;i<4;i++){ arg[i] = (char *) calloc(BUF_SIZE, sizeof (char)); } /* ok .. looking nice.. but then if i don't clear the grave after buried r (scope)deceased.. my burial ground will b filled too */ #define NO_OF_ARGS char **arg; arg = (char **) calloc (NO_OF_ARGS, sizeof(char *));
for(i=0;i<NO_OF_ARGS; i++){ arg[i] = (char *) calloc (BUF_SIZE, sizeof(char)); } /* ok.. i m digging the burial ground too !*/
caveat: if you dig, remember to cover later (think free!!)
ok.. above is just for fun.. no offence implied. -shailesh
--- Amish Mehta amish@ownmail.com wrote:
Hi,
You got divine hands :-). Though it worked, you should still allocate memory to arg itself for holding array of pointers. Something like
arg = (char **) malloc(sizeof(char *) * 6); arg[5] = NULL;
Amish.
Nikhil Joshi wrote:
Hi!
success afterall ... I used following to allocate memory to arg
for (i=1;i<4;i++) if ((arg[i] = (char *) malloc (30)) == NULL)
__________________________________________________ Do you Yahoo!? New DSL Internet Access from SBC & Yahoo! http://sbc.yahoo.com
On Thu, 26 Sep 2002, Shailesh wrote:
/* ok .. looking nice.. but then if i don't clear the grave after buried r (scope)deceased.. my burial ground will b filled too */ #define NO_OF_ARGS char **arg; arg = (char **) calloc (NO_OF_ARGS, sizeof(char *));
for(i=0;i<NO_OF_ARGS; i++){ arg[i] = (char *) calloc (BUF_SIZE, sizeof(char)); } /* ok.. i m digging the burial ground too !*/
heck, these pointers are making me feel dizzy already ;) i guess i gotta refer kanetker or balguruswamy urgently ...
(didn't study the thing properly in my regular course , in fact most of the junta leaves pointers for 'option' )
anywayz thx guys 4 the suggestions
-- Nikhil Joshi