Pointers and dinamic memory

Hello, I have to do some program, similiar to the Facebook.
I wrote the syntax, but it doesn't compile...I don't know what the problem is...
Will appreciate for any help!

http://www.turboupload.com/3i3jbpisaog5/Document.txt.html
post your code here in the code-brackets, please... nobody wants to wait 1 minute to download your code for a question that you asked for... in my opinion
Or deal with full screen popup adds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct Person {
	int id;
	char* name;
	struct PersonList* friends;
} Person;

typedef struct PersonList {
	Person* person;
	struct PersonList* next;
} PersonList;


// list of all messages to use in your program
#define USER_NOT_EXIST "User does not exist\n"
#define ALREADY_FRIENDS "But users are already friends\n"
#define NOT_FRIENDS "But users are not friends\n"
#define USER_DETAILS "Name: %s, ID: %d, Friends: %d\n"
#define LIST_COMMON "List of common friends:\n"
#define NO_COMMON "No common friends\n"
#define MENU "Please choose an option:\n========================\n1. Add user\n2. Delete user\n3. Create friendship between two users\n4. Cancel friendship between two users\n5. Print all users sorted by popularity\n6. Print common friends\n0. Exit\n\nYour choice: "
#define ENTER_NAME "Enter the user's name [up to 100 chars]: "
#define ENTER_ID "Enter user ID: "
#define ENTER_FIRST_ID "Enter first user ID: "
#define ENTER_SECOND_ID "Enter second user ID: "
#define ENTER_ID_LIST_1 "Enter ID no. 1: "
#define ENTER_ID_LIST_X "Enter ID no. %d (0 to terminate): "

int PrintMenu();
// returns a new id to use for a person
int getNewId();

// returns a person* according to id, or NULL if user doesnt exist
Person* getPersonById(int id, PersonList* list);

// adds a person to list and returns the list
PersonList* addPerson(char* personName, PersonList* list);

// returns whether two people are friends
int areFriends(Person *first, Person* second);

// removes a person. Updates all relevant lists,
// returns the updated allPersons list.
PersonList* delPerson(int id, PersonList* allPersons);

// make persons id1, id2 to be friends
void makeFriends(int id1, int id2, PersonList* allPersons);

// initialize the program with the following information:
// persons: moshe, yoel, haim, ido, adam, ronen, gilad, dana, roni, michal, adi, omer
// friends: (note that friends are mutual)
// <Moshe,Yoel>, <Moshe,Ido>,<Moshe,Roni>,<Yoel,Ronen>,<Yoel,Omer>,<Yossi,Dana>,<Yossi,Adi>,<Gilad,Roni>,
// <Gilad,Adi>,<Dana,Michal>,<Adi,Omer>.
PersonList* initDatabase();

// cancel friendship of persons id1, id2
void cancelFriends(int id1, int id2, PersonList* allPersons);

// returns the number of friends of a person
int numOfFriends(Person* person);

// returns a sorted list of people by popularity and then lexicographically
PersonList *sortByPopularity(PersonList* allPersons);

// prints all people by popularity
void printPeopleByPopularity(PersonList* allPersons);

// prints common friends of persons,
// will also work if persons is a list of 1 person, in that case it will just print this person's friends.
void printCommonFriends(PersonList* persons);

void delFriend(Person *person,Person *buddy);

void addFriend (Person* person, PersonList* list);

int PrintMenu()
{
	int choise=10;
	do{
	printf("Please choose an option:\n");
	printf("========================\n");
	printf("1. Add user\n");
	printf("2. Delete user\n");
	printf("3. Create friendship between two users\n");
    printf("4. Cancel friendship between two users\n");
    printf("5. Print all users sorted by popularity\n");
    printf("6. Print common friends\n");
    printf("0. Exit\n\n");
    printf("Your choice:");
	scanf("%d",&choise);
	}
	while(choise>6 || choise<0);
	return choise;
}
// returns a new id to use for a person
int getNewId()
{
	static int id = 1;
	return id++;
}

// initialize the program with the following information:
// persons: moshe, yoel, haim, ido, adam, ronen, gilad, dana, roni, michal, adi, omer
// friends: (note that friends are mutual)
// <Moshe,Yoel>, <Moshe,Ido>,<Moshe,Roni>,<Yoel,Ronen>,<Yoel,Omer>,<Yossi,Dana>,<Yossi,Adi>,<Gilad,Roni>,
// <Gilad,Adi>,<Dana,Michal>,<Adi,Omer>.
PersonList* initDatabase() {
	PersonList *PList = NULL;
	PList = addPerson("Moshe", PList);
	PList = addPerson("Yoel", PList);
	PList = addPerson("Haim", PList);
	PList = addPerson("Ido", PList);
	PList = addPerson("Yossi", PList);
	PList = addPerson("Ronen", PList);
	PList = addPerson("Gilad", PList);
	PList = addPerson("Dana", PList);
	PList = addPerson("Roni", PList);
	PList = addPerson("Michal", PList);
	PList = addPerson("Adi", PList);
	PList = addPerson("Omer", PList);

	makeFriends(1,2, PList);
	makeFriends(1,4, PList);
	makeFriends(1,9, PList);
	makeFriends(2,6, PList);
	makeFriends(2,12, PList);
	makeFriends(5,8, PList);
	makeFriends(5,11, PList);
	makeFriends(7,9, PList);
	makeFriends(7,11, PList);
	makeFriends(8,10, PList);
	makeFriends(11,12, PList);

	return PList;
}

void main(){
	char User[100];
	int choise,id1,id2;
	PersonList *allPersons;
	allPersons=initDatabase();
	do
	{
	choise=PrintMenu();
	switch(choise)
	{
	case 1:
		printf("Enter the user's name [up to 100 chars]: ");
		gets(User);
		addPerson(User,allPersons);
		break;
	case 2:
		printf("Enter user ID: ");
		scanf("%d",&id1);
		if(getPersonById(id1,allPersons))
		{
			delPerson(id1,allPersons);
			break;
		}
	    else printf("User does not exist\n");
		break;
	case 3:
		printf("Enter first user ID: ");
        scanf("%d",&id1);
        printf("Enter second user ID: 2");
		scanf("%d",&id2);
		makeFriends(id1,id2,allPersons);
		break;
	case 4:
		printf("Enter first user ID: ");
        scanf("%d",&id1);
        printf("Enter second user ID: ");
		scanf("%d",&id2);
		cancelFriends(id1,id2,allPersons);
		break;
	case 5:
		printPeopleByPopularity(allPersons);
	    break;
	case 6:
		printCommonFriends(allPersons);
		break;
	}
	while(choise);
}

PersonList* addPerson(char* personName, PersonList* list){
	Person *newP;
	PersonList *newL;

	newP=(Person*)malloc( sizeof(Person) );
	newP->name=(char*)malloc( sizeof(char)*(strlen(personName)+1) );
	strcpy(newP->name,personName );
	newP->id=getNewId();
	newP->friends=NULL;
	
	newL=(PersonList*)malloc( sizeof(PersonList) );
	newL->person=newP;
	newL->next=list;

	return newL;
}

PersonList* delPerson(int id, PersonList* allPersons){
PersonList* tempL1,*tempL2,*tempF,*tempF1;


	for(tempL1=allPersons,tempL2=tempL1;tempL1!=NULL && id!=tempL1->person->id;tempL2=tempL1,tempL1=tempL1->next);

	if (tempL1==NULL)
		printf(USER_NOT_EXIST);

	for(tempF=tempL1->person->friends;tempF!=NULL;)
	{
		delFriend(tempF->person,tempL1->person);
		tempF1=tempF;		
		tempF=tempF->next;
		free(tempF1);
	}

	free(tempL1->person);
	
	if (tempL1==tempL2)
	{
		tempL1=tempL1->next;
		free(tempL2);
		return tempL1;
	}
	else
	{
		tempL2->next=tempL1->next;
		free(tempL1);
		return allPersons;
	}



}

void delFriend(Person *person,Person *buddy){
PersonList* tempL1,*tempL2;

	
	for(tempL1=person->friends,tempL2=tempL1; tempL1!=NULL && tempL1->person!=buddy;tempL2=tempL1,tempL1=tempL1->next);
	
	if(tempL1!=NULL)
	{
		if (tempL2!=tempL1)
		{
			tempL2->next=tempL1->next;
			free(tempL1);
		}
		else
		{
			person->friends=tempL1->next;
			free(tempL1);
		}

	}

}

Person* getPersonById(int id, PersonList* list)
{
	PersonList* tempL1;

	for(tempL1=list;tempL1!=NULL && id!=tempL1->person->id;tempL1=tempL1->next);
	if (tempL1==NULL)
		return NULL;
	else
	return tempL1->person;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
void makeFriends(int id1, int id2, PersonList* allPersons)
{
	Person* person1,*person2;
	PersonList* list1,*list2;

	person1=getPersonById(id1, allPersons);
	person2=getPersonById(id2, allPersons);
	list1=(PersonList*)malloc( sizeof(PersonList) );
	list2=(PersonList*)malloc( sizeof(PersonList) );
	list1->person=person2;
	list2->person=person1;
	addFriend (person1, list1);
	addFriend (person2, list2);
}
void addFriend (Person* person, PersonList* list)
{
	PersonList* tempL1,*tempL2;

	for(tempL1=person->friends,tempL2=tempL1; tempL1!=NULL;tempL2=tempL1,tempL1=tempL1->next);
	if (tempL1==tempL2)
		person->friends=list;
	else
		tempL2->next=list;

}

int areFriends(Person *first, Person* second)
{
PersonList* list1,*list2;

for(list1=first->friends; list1!=NULL && list1->person!=second;list1->next);
if (list1==NULL)
	return 0;
for(list2=second->friends; list2!=NULL && list2->person!=first;list2->next);
if (list2==NULL)
	return 0;
return 1;

}

void cancelFriends(int id1, int id2, PersonList* allPersons)
{
	Person* person1,*person2;

	person1=getPersonById(id1, allPersons);
	person2=getPersonById(id1, allPersons);
	delFriend( person1, person2);
	delFriend( person2, person1);

}

int numOfFriends(Person* person)
{
	int i;
	PersonList* list1;

	for(i=0,list1=person->friends; list1!=NULL;i++, list1->next);

	return i;
}

PersonList *sortByPopularity(PersonList* allPersons)
{
	PersonList* list1,*list2,*list3;
	Person* tempP1;

	list3=NULL;
	while(list3!=allPersons)
	{
		list2=allPersons;
		if (list2->next!=NULL)
			list1=list2->next;
		else 
			return allPersons;

		while (list1!=list3)
		{
			if( strcpm(list1->person->name,list2->person->name)>0 )
			{
				tempP1=list1->person;
				list1->person=list2->person;
				list2->person=tempP1;
			}

			list2=list1;
			list1=list1->next;
		}
		if (list1==list3)
		{
			list3=list2;
		}

	}
}


Thanks in advance
If you correct the indentation in main, you'll see that the switch statement doesn't end correctly.
Thanks!!!
Now I have another problem:
The function "addFriend" makes me problems...
The debugger directs me to the "for" loop, there some problem with the values of tempL1 and tempL2
I don't understand what does this thing want!
person->friends=list;
Are you sure you want to do that? Aren't you supposed to be adding person to list and not the other way around?

Ave you considered using STL containers?
This is stiil me, but from another username.
the problem is not in this line, but in the line of "for" declaration
Topic archived. No new replies allowed.