• C

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;
typedef struct {
	char* commands[MAX_COMMANDS];
	int active[MAX_COMMANDS];
	char* arguments[MAX_ARGUMENTS];
	int counter;
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];
			return true;
			return false;
int main(int argc, char **argv) {
	OPTIONS options;
	PHONEBOOK myPhonebook;
	char fileName[] = "phonebook.csv";
	load(fileName, &myPhonebook);
	myPhonebook.linkedList->counter = 0;
	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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

where is PHONEBOOK declared?
where is myPhonebook.linkedList initialized?
also, if argc > MAX_ARGUMENTS+2,  options->arguments[argumentIndex - 2] could be out of bounds
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;
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];
	while(fscanf(phonebook, "%s", line) != EOF)
		parseInput(line, firstName, lastName, phoneNumber);
		newentry(myPhonebook->linkedList, firstName, lastName, phoneNumber);

Open in new window

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

       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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You could also use a static local instead of an auto local
I don't see the linklist NODEs being initialized
bejhanAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.