Link to home
Start Free TrialLog in
Avatar of bejhan
bejhan

asked on

C Program values somehow being changed (unvoluntarily)

I am writing a program to be a command line phonebook using a linkedlist manager. I'm pretty new to C (much more experience in C++ and Java) so I'm battling through these segmentation faults and I'm stuck in a particular spot. I have included the problematic section of my code.

In the main function, from the first printf to the second printf the value of myPhonebook.linkedList->counter changes from 0 to 2280520. The only thing between these two statements is the parse function which, as you can see, has nothing to do with this value. Why is this happening. How can I fix this problem.
typedef struct {
	NODE *firstNode;
	NODE *lastNode;
	NODE *currentNode;
	int counter;
} LINKEDLIST;
 
typedef struct {
	char* commands[MAX_COMMANDS];
	int active[MAX_COMMANDS];
	char* arguments[MAX_ARGUMENTS];
	int counter;
} OPTIONS;
 
bool parse(OPTIONS *options, int argc, char **argv) {
 
	if(argc <= 1)
		return false;
	else {
		int optionIndex;
		bool  found = false;
		for(optionIndex = 0; optionIndex < options->counter && !found; optionIndex++) { //until the end of the options list
 
			if(strcmp(argv[1], options->commands[optionIndex]) == 0) {
				found = true;
				options->active[optionIndex] = 1;
 
				int argumentIndex;
				for(argumentIndex = 2; argumentIndex < argc; argumentIndex++)
					//strcpy(options->arguments[argumentIndex - 2], argv[argumentIndex]);
					options->arguments[argumentIndex - 2] = argv[argumentIndex];
			}
		}
 
		if(found)
			return true;
		else
			return false;
	}
}
 
int main(int argc, char **argv) {
 
	OPTIONS options;
	PHONEBOOK myPhonebook;
	char fileName[] = "phonebook.csv";
 
	load(fileName, &myPhonebook);
	myPhonebook.linkedList->counter = 0;
 
	initialize(&options);
	addCommand(&options, "newentry");
	addCommand(&options, "displayall");
	addCommand(&options, "remove");
	printf("\n%i", myPhonebook.linkedList->counter);
	/*bool parseResult = */parse(&options, argc, argv);
	printf("\n%i", myPhonebook.linkedList->counter);
 
return 0
 
}

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

where is PHONEBOOK declared?
where is myPhonebook.linkedList initialized?
also, if argc > MAX_ARGUMENTS+2,  options->arguments[argumentIndex - 2] could be out of bounds
Avatar of bejhan
bejhan

ASKER

Sorry forgot to attach that part.

Yes, if argc is greater... I will put a check in for that. But as I am testing that won't be an issue, max_arguments is defined as 10.
typedef struct {
	LINKEDLIST *linkedList;
} PHONEBOOK;
 
void load(char *fileName, PHONEBOOK *myPhonebook) {
	LINKEDLIST myLinkedList;
	myPhonebook->linkedList = &myLinkedList;
	myPhonebook->linkedList->counter = 0;
 
	FILE *phonebook = fopen(fileName, "r");
 
	if(phonebook == NULL) {
		printf("Error: Cannot open file for reading.");
		exit (-1);
	}
 
	char firstName[MAX_NAME_LENGTH];
	char lastName[MAX_NAME_LENGTH];
	char phoneNumber[PHONE_NUMBER_LENGTH];
	char line[MAX_NAME_LENGTH + MAX_NAME_LENGTH + PHONE_NUMBER_LENGTH];
	while(fscanf(phonebook, "%s", line) != EOF)
	{
		parseInput(line, firstName, lastName, phoneNumber);
		newentry(myPhonebook->linkedList, firstName, lastName, phoneNumber);
	}
 
	fclose(phonebook);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could also use a static local instead of an auto local
I don't see the linklist NODEs being initialized
Avatar of bejhan

ASKER

Awesome.