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

LVL 1
bejhanAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
       LINKEDLIST myLinkedList;
        myPhonebook->linkedList = &myLinkedList;

myPhonebook->linkedList now points to a local LINKEDLIST ... that will no longer be valid when the function returns !!! Don't do this. Allocate memory for the LINKEDLIST using calloc or so.
0
 
ozoCommented:
where is PHONEBOOK declared?
where is myPhonebook.linkedList initialized?
0
 
ozoCommented:
also, if argc > MAX_ARGUMENTS+2,  options->arguments[argumentIndex - 2] could be out of bounds
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
bejhanAuthor Commented:
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

0
 
ozoCommented:
You could also use a static local instead of an auto local
I don't see the linklist NODEs being initialized
0
 
bejhanAuthor Commented:
Awesome.
0
All Courses

From novice to tech pro — start learning today.