I'm trying to sort a list of names alphabetically, by isolating the first element of the string and compare, which I think should work, but there's an error, SISEGV with something about an invalid access to storage... I don't know, but my group mates and I can't figure this out. Can anyone spot anything wrong? Help appreciated. Thanks!

void addGalaxy(ptrUniverse *pFirstG)
{	char 	cChoice, 
			cDump;
	ptrUniverse 	pNewG, pNextG, pPrevG;
	int				nElem = 0, nEnd;
	
	pNewG = malloc(sizeof(struct galaxyTag));

	do{
		printf("Do you want to add a galaxy? [y/n]");
		scanf("%c%c", &cChoice, &cDump);
		if(cChoice == 'Y' || cChoice == 'y')
		{
			printf("Enter Name of Galaxy: ");
			gets(pNewG->strGalaxyName);
			addSystem(&pNewG->pSolarSystem);
			pNewG->pNextGalaxy = NULL;
			if(*pFirstG = NULL)
				*pFirstG = pNewG;
			else 
			{	
				pNextG = pNewG;
				while(pNewG->strGalaxyName[nElem] != '\0' && pNextG->strGalaxyName[nElem] != '\0')
				{
					while(pNewG->strGalaxyName[nElem] > pNextG->strGalaxyName[nElem] && pNextG->pNextGalaxy != NULL)
					{
						pPrevG = pNextG;
						pNextG = pNextG->pNextGalaxy;
					}
					if (pNextG->strGalaxyName[nElem] > pNewG->strGalaxyName[nElem])
					{
						pPrevG->pNextGalaxy = pNewG;
						pNewG->pNextGalaxy = pNextG;
					}
					else if (pNextG->strGalaxyName[nElem] < pNewG->strGalaxyName[nElem])
						pNextG->pNextGalaxy = pNewG;
						nElem++;
				}
			}
		}
	}while(cChoice != 'n' || cChoice != 'N');
}

SIGSEGV is a memory access violation --meaning that you tried to read (or write, but in your case read) memory that doesn't belong to you.

The problem lies in lines 23 and 25. The condition at 23 is blissfully ignored by the loop at 25, which will continue until you try to read past your accessible memory.

You can use the strcmp() function to compare strings instead of doing it yourself.

You haven't given very much code or the structure of your galaxy, so it is hard to examine more deeply than that.

Hope this helps.

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.