Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
Two codes from different programs. Each should be exactly the same. No problems in one; the other fails at strcpy(arr[fld],p) in parse which runs when it is commented out. Do see anything I have missed? Any suggestions on how to debug? Could it be entirely unrelated to this even though that's where it fails.


from one program:
Code:
arr = (char **)calloc(100, sizeof(char*));
		for(j = 0; j < 100; j++)
			arr[j] = (char *)calloc(10,sizeof(char));

fscanf(fp, "%s\n", listname); //get first file
		action = fopen(listname, "r"); //open first file
		fscanf(action, "%s\n", line); // discard first lineprintf("%s\n",  arr[i]);
		
		printf("%s\n", listname);
		while (fgets(tmp,1024,action) != 0)
		{
		parse(tmp,",",(char**)arr,&fldcnt); // whack record into fields 




void parse( char *record, char *delim, char **arr,int *fldcnt)

{

	char* p=strtok(record,delim);
	int fld=0;
	while(p)
	{
		strcpy(arr[fld],p);
		fld++;
		p=strtok('\0',delim);	
	}


}

from another code

Code:
	arr = (char **)calloc(100, sizeof(char*));
	for (i = 0; i < 100; i ++)
		arr[i] = (char*)calloc(10, sizeof(char));
	
		fscanf(fl, "%s\n", listname); //get first file
		printf("%s\n", listname);
		if ((action = fopen(listname, "r")) == NULL)
			printf("can't open action\n");
		fscanf(action, "%s\n", line); // discard first line
	
			while (fgets(tmp,1024,action) != 0)
			{	
				parse(tmp,",",(char**)arr,&fldcnt);

void parse( char *record, char *delim, char **arr,int *fldcnt)


{
	
	char* p=strtok(record,delim);
	int fld=0;
	while(p)
	{printf("%d %s\n", fld, arr[fld]);
		//strcpy(arr[fld],p);
		fld++;
		
		p=strtok('\0',delim);	
	}
	
	
}
 
If it's running when it's commented out, then the code you're changing isn't the code that's being run--even if it's where the debugger points you.


Clean and rebuild.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.