On Friday 27 September 2002 07:40 am, Nikhil Joshi wrote:
if ((*arg = (char *) malloc (30)) == NULL) { printf ("Error allocating memory \n"); exit (1); }
Boy, you're not advancing the "arg" pointer.... so, it is something like
arg[0] = (char *)malloc(30);
I guess, this is not what you want, right??? In this case, what you need to do is advance the arg pointer. Something like
for( i =0 ; i < 4; i++) { if((*(arg + i) = (char *)malloc(n * sizeof(char))) == NULL) { perror("Mem Allocation Error...\n"); exit(1); } }
I guess, in your earlier program, you'd done something like
for(i = 0; i < 4; i++) if((arg[i] = (char *)malloc(30)) == NULL)
I don't see any point why this should give you Segmentation fault. It seems to work for me though.... It is exactly what the above code will do, using the offset alongwith the pointer.
But, if you want a simple and better way, I suggest you use C++. The string class (which in fact is a typedef to the base_string<char> template) is simple to use. Also, it will handle the memory allocation problems internally. Refer to the documentation for more details.