Hey everyone,

I'm attempting to write a program that will contain multiple C-DLLs. I currently have 2 structs:

struct CDLL_Node {
	char value[30];
	struct CDLL_Node *next;
	struct CDLL_Node *prev;
};
typedef struct CDLL_Node node;

struct listHolderRecord {
	char label[30];
	struct listHolderRecord *next;
	node *headAddr;
};
typedef struct listHolderRecord listHolder;

The first will be for creating C-DLL's, the second will be for storing them in a SLL It will hold a label for each and the address of the list that that label corresponds to. I can create the C-DLL no problem. What I'm having problems with is when I try to add the head pointer to the second list.

I pass it as a parameter through a function as shown below.

listOfLists->headAddr = headNode;

For some reason, it crashed right here. I know the headNode is correct because the line above it I can print values out from it. I just dont know why it wont let me assign it to the "listOfLists->headAddr" var. Could anyone point me in the right direction as to how to fix this? I've been trying to for hours.

Any help would be much appreciated.

Thanks!
Barefoot

Recommended Answers

All 10 Replies

>For some reason, it crashed right here.
The only reason for a crash there is if listOfLists can't be dereferenced (if it's NULL, for example).

Hmm, it just might be NULL there. Further debugging made me find out that as soon as I declare it I cant even access it. I attempted this in main and it crashed:

listHolder *listHolderPtr;
strcpy(listHolderPtr->label,"Hello world");

Am I declaring it the wrong way or something?

Thanks for the quick feedback

Hey everyone,

I pass it as a parameter through a function as shown below.

listOfLists->headAddr = headNode;

For some reason, it crashed right here.

In general, if a program crashes when you dereference a pointer (e.g. -> operator) then the pointer is not valid. Probably you didn't initialize listOfLists properly. A self-contained code example that reproduces the crash would make it easy to diagnose this. Also, any debugger would probably show you the problem immediately.

btw, I don't know what the phrase about parameters and functions means; what you show is an assignment statement.

HTH,

-Dan

>Am I declaring it the wrong way or something?
You've just given a classic example of misunderstanding pointers. Just because you've defined a pointer doesn't mean you can use it right away. You have to point it to a suitable block of memory first. Right now your listHolderPtr is indeterminate. It could point anywhere, and hopefully it points somewhere that'll crash immediately when you dereference it rather than linger until it's painfully expensive to fix the problem.

>Am I declaring it the wrong way or something?
You've just given a classic example of misunderstanding pointers. Just because you've defined a pointer doesn't mean you can use it right away. You have to point it to a suitable block of memory first. Right now your listHolderPtr is indeterminate. It could point anywhere, and hopefully it points somewhere that'll crash immediately when you dereference it rather than linger until it's painfully expensive to fix the problem.

Ahh, thank you. I completely forgot about "malloc". I use it with the other list but it just slipped my mind. Now, since the listOfLists will have 3 datatypes, the pointer to the next, the head pointer of type "node" , and a label of type char, what should I allocate memory too?

>the pointer to the next
Should probably be NULL unless you already have the next node available to link.

>the head pointer of type "node"
Which you already have because you're trying to assign it.

>and a label of type char
Which isn't exactly a label. If it's an array you can leave it, but if it's a pointer to char, you should probably allocate some memory to it before copying a string in there.

>what should I allocate memory too?
My guess would be something like this:

listHolder *listHolderPtr = malloc ( sizeof *listHolderPtr );

if ( listHolderPtr != NULL ) {
  listHolderPtr->label = malloc ( <a suitable size> );

  if ( listHolderPtr->label != NULL ) {
    strcpy ( listHolderPtr->label, "Hello world" );
    ...
  }
}

Wow, thanks for the quick response. It solved my problem. However, something else is not working. Below is just a small segment of the code i was working on. It gets passed an empty listOfLists so it goes into this section. It works but now, when it returns to the calling function, listOfLists->label, contains NULL where it should contain whatever listLabel was. Am I not passing the right thing to the function or making another mistake somewhere else?

void addHeadInfo(listHolder *listOfLists, node *headNode, char *listLabel) {	
	if(listOfLists == NULL) {
		//Allocate memory for the list label
		listOfLists = malloc(sizeof(listLabel));
		listOfLists = malloc(sizeof(*headNode));	
		
		listOfLists->next = NULL;
		listOfLists->headAddr = headNode; 
		printf("the label being copied is %s", listLabel);
		strcpy(listOfLists->label,listLabel);
		printf("\n\nthe label in memory is %s\n\n",listOfLists->label);

		return;
	}
}

BTW, those printf statements print out exactly what they should. So it prints the label given then the label in memory which is exactly what it should do.

Thanks again for the speedy response and I hope you can do the same w/ this problem!

:)

>Am I not passing the right thing to the function or making another mistake somewhere else?
Both, actually. I'll give you a few hints:

1) A pointer is a variable that holds an address.
2) All parameters in C are passed by value (they're copied).

Did you figure it out? If you copy a pointer, then allocate memory to the copy, you've done jack squat to the original pointer. There are two good solutions:

1) Pass a pointer to the pointer to your function. Then allocate memory to the dereferenced pointer.
2) Return the newly allocated pointer and assign it to the original.

I would just like to add that this thread indicates that you would save a lot of time in the long run by picking up a C reference book and thoroughly reading and working through all the exercises in the section(s) about pointers. I don't mean this as an insult, as we all didn't understand pointers at one point. But my experience is that if you are going to program in C you need a deep understanding of pointers, not a cookbook of approaches that usually work. One of my favorites is Expert C Programming by Peter van der Linden. Most of us would do well to read that one.

-Dan

Hey everyone,

Thanks for all the help!

-Barefoot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.