Click here to Skip to main content
14,422,107 members

C / C++ / MFC

 
Questionlinked list in C Pin
a random user7-Jun-15 5:28
Membera random user7-Jun-15 5:28 
AnswerRe: linked list in C Pin
Chris Losinger9-Jun-15 5:55
professionalChris Losinger9-Jun-15 5:55 
Questionneed help with linked structs at C language Pin
a random user6-Jun-15 0:57
Membera random user6-Jun-15 0:57 
AnswerRe: need help with linked structs at C language Pin
Richard MacCutchan6-Jun-15 1:29
communityengineerRichard MacCutchan6-Jun-15 1:29 
GeneralRe: need help with linked structs at C language Pin
a random user6-Jun-15 1:56
Membera random user6-Jun-15 1:56 
GeneralRe: need help with linked structs at C language Pin
Frankie-C6-Jun-15 7:42
MemberFrankie-C6-Jun-15 7:42 
GeneralRe: need help with linked structs at C language Pin
a random user6-Jun-15 8:03
Membera random user6-Jun-15 8:03 
GeneralRe: need help with linked structs at C language Pin
Frankie-C6-Jun-15 8:12
MemberFrankie-C6-Jun-15 8:12 
Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd().
I report the whole program here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct numberNode {
	int number;
	struct numberNode *Next;
};

typedef struct numberNode numStruct;

numStruct *createSong(int numbers)
{
	numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
	if (newSong)
	{
		newSong->number = numbers;
		newSong->Next = NULL;
	}
	return newSong;
}

void insertAtEnd(numStruct **firstNode, numStruct *newNode)
{
	numStruct *currNode = *firstNode;

	// if the linked list is empty
	// should put the new node as the first
	if (!currNode)
	{
		*firstNode = newNode;
		newNode->Next = NULL;
	}
	else
	{
		while (currNode->Next)	// problem at the second loop
		{
			currNode = currNode->Next;
		}

		currNode->Next = newNode;
		newNode->Next = NULL;
	}
	printf("\n\n");
}

numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
{
	int count = 0;
	int numbers;

	printf("\nPlease enter numbers and at the end enter -999\n");

	do
	{
		count++;
		//flushall();
		fflush(stdin);
		numbers = 0;

		printf("\n\n%d.\n\nnumber: ", count);
		scanf("%d", &numbers);

		if (numbers != -999)
		{
			newNode = createSong(numbers);
			insertAtEnd(anchorNode, newNode);
		}
	}
	while (numbers != -999);
	printf("\n\n");
	return NULL;
}

void printList(numStruct *firstNode)
{
	numStruct *currSong = firstNode;
	printf("\n\n-------------------------------------\n");
	while (currSong)
	{
		printf("number= %d \n", currSong->number);
		currSong = currSong->Next;
	}
	printf("-------------------------------------\n\n");
	printf("\n\n");
}

int main(void)
{
	numStruct *anchorNode = NULL;
	numStruct *newNode = NULL;
	AddSongs(&anchorNode, newNode);
	printList(anchorNode);

	system("PAUSE");
}

Check it against your code.
We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.
GeneralRe: need help with linked structs at C language Pin
a random user6-Jun-15 8:20
Membera random user6-Jun-15 8:20 
GeneralRe: need help with linked structs at C language Pin
a random user6-Jun-15 8:25
Membera random user6-Jun-15 8:25 
GeneralRe: need help with linked structs at C language Pin
Frankie-C7-Jun-15 6:33
MemberFrankie-C7-Jun-15 6:33 
GeneralRe: need help with linked structs at C language Pin
a random user7-Jun-15 7:32
Membera random user7-Jun-15 7:32 
GeneralRe: need help with linked structs at C language Pin
Frankie-C7-Jun-15 8:38
MemberFrankie-C7-Jun-15 8:38 
QuestionRe: need help with linked structs at C language Pin
David Crow8-Jun-15 4:20
MemberDavid Crow8-Jun-15 4:20 
AnswerRe: need help with linked structs at C language Pin
a random user21-Jun-15 10:37
Membera random user21-Jun-15 10:37 
QuestionThreading Issues Pin
ForNow5-Jun-15 6:09
MemberForNow5-Jun-15 6:09 
AnswerCAsyncSocket notifications Onconnect OnSend owned by main thread Pin
ForNow7-Jun-15 3:15
MemberForNow7-Jun-15 3:15 
GeneralRe: Threading Issues Pin
Albert Holguin8-Jun-15 17:21
professionalAlbert Holguin8-Jun-15 17:21 
GeneralRe: Threading Issues Pin
ForNow8-Jun-15 18:21
MemberForNow8-Jun-15 18:21 
Questionwhy 4 twice Pin
Member 117446405-Jun-15 5:15
MemberMember 117446405-Jun-15 5:15 
AnswerRe: why 4 twice Pin
David Crow5-Jun-15 5:19
MemberDavid Crow5-Jun-15 5:19 
AnswerRe: why 4 twice Pin
k50545-Jun-15 6:35
Memberk50545-Jun-15 6:35 
AnswerRe: why 4 twice Pin
Frankie-C6-Jun-15 7:56
MemberFrankie-C6-Jun-15 7:56 
AnswerRe: why 4 twice Pin
Daniel Pfeffer7-Jun-15 4:15
professionalDaniel Pfeffer7-Jun-15 4:15 
GeneralRe: why 4 twice Pin
Stefan_Lang11-Jun-15 4:14
mveStefan_Lang11-Jun-15 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.