• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

quick C programming question

LiLRiceBoi

Golden Member
this program is supposed to parse a string of integers entered and make a linked list. the function that i'm having problems on is adding "atoms" to a list. ex. ( 1 2 3 ) has 3 atoms, 1, 2, and 3, while the list ( 1 ( 2 3 ) ) has only two elements, one atom, 1, and a sublist, ( 2 3 ).

here is the struct and the function that i'm having problems on


typedef struct atomStruct {
int value; /* let's just use ints for the atom's value */
struct atomStruct* sublistPtr; /* if the element is really a sublist */
struct atomStruct* nextPtr; /* the next element in the list (or NULL if end) */
} AtomType, *AtomTPtr;

/* AddAtomToList - Add an atom to the end of the list
* Inputs:
* list - the list (possibly NULL - empty)
* atom - the atom to add
* Outputs: the pointer to the head of the list
* Side Effects: The atom is added to the end of the list. If the list was empty,
* the atom will be the only member of the list.
*/
AtomTPtr AddAtomToList(AtomTPtr list, AtomTPtr atom)
{
AtomTPtr newlist; /*new list of same type*/
if (list == NULL) /*if the list is empty make it equal atom*/
list = atom;
else /*if it's not empty*/
{
newlist = list; /*make newlist so list still points to head*/
while (newlist != NULL) /*find end of list*/
{
newlist = newlist->nextPtr;
}
}

newlist->nextPtr = atom; /*set the end of newlist, which points to list, = atom*/

return list; /*return list, which still points to the head of list*/
}




i commented so you can quickly see what's going on. visual studios says the problem is on the second to last line, the one with newlist->nextPtr = atom;. I would appreciate any help i can get on this. thanks.
 
It's been a while since I've done any programming, but should
while(newlist != NULL)
be changed to "while(newlist->nextPtr != NULL)"? It looks to me like you could be going through the while loop one too many times.
 
ok well it seems you are not gonna respond before i goto sleep

so here is my 1 minute glance at it:

if your code goes in the else block then the pointer variable newlist gets set as list.

if it doesnt get set it will be a null pointer and you will try and derefernece null because you never gave it an intial value in the if block

but without know whtere the error you are getting is compile time or run time or what the error is, i am not gonna think about it too hard.

next time give more info.

edit: plus the lack of tabs makes the code hard to read in my tired state.
 
May I ask why are you passing AtomTPtr list parameter by value?
You should pass a pointer to it, operate on pointer and return a pointer.

You mix and match AtomTPtr* and AtomTPtr, hence the source of your troubles, I believe
 
Here's a revised version which compiles fine:

struct atomStruct {
int value;
struct atomStruct* sublistPtr;
struct atomStruct* nextPtr;
};

atomStruct AtomType;

atomStruct* AddAtomToList(atomStruct* list, atomStruct* atom)
{
atomStruct* newlist;
if (list == 0) {
list = atom;
}
else {
newlist = list;
while (newlist != 0)
{
newlist = newlist->nextPtr;
}
}

newlist->nextPtr = atom;
return list;
}

int main ()
{
// nothing to do
return 0;
}
 
it compiles fine the way it is. i am not passing by value. if you look where i first declared the struct, it is a pointer.

the error i'm getting while trying to debug using visual studios is "Unhandled exception in lists.exe: 0xc000005: Access Violation."
 
while (newlist != NULL) /*find end of list*/
{
newlist = newlist->nextPtr;
}

will end with newlist equal to NULL. So you won't be able to access the newlist->nextptr, so there is the access violation (that 0005 error appears when dereferencing NULL pointers).
You should check for newlist->nextPtr, not newList in the while condition.

Calin

P.S. You should use the "pointer to pointer technique" to parse the list:
ppNewList = &list;
while (*ppNewList != NULL)
{
ppNewList = &(*ppNewList->nextPtr);
}
//You exit with ppNewList == NULL, and then
ppNewList = atom;
 
Originally posted by: LiLRiceBoi
it compiles fine the way it is. i am not passing by value. if you look where i first declared the struct, it is a pointer.

the error i'm getting while trying to debug using visual studios is "Unhandled exception in lists.exe: 0xc000005: Access Violation."

sounds like null pointer violation, look at my original post
 
Here's my fix to your (likely) problem. My stuff is bolded.
typedef struct atomStruct {
int value; /* let's just use ints for the atom's value */
struct atomStruct* sublistPtr; /* if the element is really a sublist */
struct atomStruct* nextPtr; /* the next element in the list (or NULL if end) */
} AtomType, *AtomTPtr;

/* AddAtomToList - Add an atom to the end of the list
* Inputs:
* list - the list (possibly NULL - empty)
* atom - the atom to add
* Outputs: the pointer to the head of the list
* Side Effects: The atom is added to the end of the list. If the list was empty,
* the atom will be the only member of the list.
*/
AtomTPtr AddAtomToList(AtomTPtr list, AtomTPtr atom)
{
AtomTPtr newlist; /*new list of same type*/
if (list == NULL) /*if the list is empty make it equal atom*/
list = atom;
list->nextPtr = NULL; //clear out any garbage values
else /*if it's not empty*/
{
newlist = list; /*make newlist so list still points to head*/
while (newlist->nextPtr != NULL) /*find end of list*/
{
newlist = newlist->nextPtr;
}
newlist->nextPtr = atom; /*set the end of newlist, which points to list, = atom*/
newlist->nextPtr->nextPtr = NULL; //clear out garbage values

}


return list; /*return list, which still points to the head of list*/
}
The access violation, like others have said, is probably because your program tries to assign a value to NULL, which is forbidden in C. (In some other languages it isn't) I've also done stuff to make sure you don't inadvertantly refference a garbage pointer.

Enjoy.
 
Back
Top