80
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Embed Size (px)

Citation preview

Page 1: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Pointers 2COP3275 – PROGRAMMING USING C

DIEGO J. RIVERA-GUTIERREZ

Page 2: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative stuff

• Quiz on Friday• Easy question on pointers (regarding basic operators + malloc) (15

pts)• Create one function that does an operation to the list we will create

today (15 pts)• String function (15 pts)• Free credit question. (Every answer is correct for this one – 5pts)

• Homework #5 will be posted tonight.

Page 3: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Homework #5

• Allow different sizes of boards!

• Move seed, # of mines and board size to console arguments.• -s for seed• -m for mines• -r for rows• -c for columns

• Include mine counter (#mines – mines assigned)

• Winning condition – including when all unopened tiles are mines

• Due on July 24th.

Page 4: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Let’s build a better list…

• We have a struct that saves all the info for a student (name, UFID, DOB, major, year, etc).

struct student{char *name;int UFID;char major[3];struct Date DOB;int year;

};

Page 5: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Let’s build a better list…

• For simplicity let’s only use name

struct student{char *name;struct student *next;

};

Page 6: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Remember what we are after:

Ana Andrew Cristina Juliana Michael

Page 7: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

On Monday – Creation of the list

struct student ana;ana.name = "Ana";

struct student andrew;andrew.name = "Andrew";

ana.next = &andrew;

• That works fine. But it’s not ideal. • Very manual..

• We want functions to handle operations so we likely want an “add” function.

Page 8: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

What functions we need?

• Add• Add keeping alphabetical order!

• Delete

• Search?

Page 9: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

The add function

• What are our parameters?• Current list• Element to add

• What’s the current list at the very beginning?

• What would you consider to be an “empty” list?• NULL!

• Do we need a return value?• Yes! Why? To be able to delete the front!

Page 10: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Add function

• Return type: struct student *

• Name: Add

• Parameter 1: struct student * current_list

• Parameter 2: ?• char * name?• struct student s1?• struct strudent *s2?

Page 11: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Add functionstruct student *add(struct student *list,

char* name) {

struct student s;s.name = name;s.next = NULL;

if(list == NULL) {

return &s;

}else {

struct student *pos = list;

while((*pos).next != NULL) {pos = (*pos).next;}

(*pos).next = &s;return list;}

}

Note: This has a major error!

Page 12: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Before we get to the error – The ‘->’ operator

• The notation (*pos).next is kind of annoying.

• Having to “de-reference” the pointer to access the struct’s elements is a very common task

• We have a special operator that does this! The ‘->’ operator

• So if ptr is a pointer to a struct that has id as one of the its elements:

ptr->id is equivalent to (*ptr).id

pos->next is equivalent to (*pos).next

Page 13: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

The -> operator struct student *add(struct student *list,

char* name) {

struct student s;s.name = name;s.next = NULL;

if(list == NULL) {

return &s;

}else {

struct student *pos = list;

while(pos->next != NULL) {pos = pos->next;}

pos->next = &s;return list;}

}

Note: This has a major error!

Page 14: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

If we compile that, we get a warning about the error!

list.c:17:3: warning: function returns address of local variable [-Wreturn-local-addr]

return &s;

^

• The warning is pretty clear.

• We are returning the memory address of a “local variable”

• If you remember from our discussion on scopes:

• A “local variable” is a variable defined inside a function• As opposed o a “global variable” that is accessible in the full scope

of the program

Page 15: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

So what’s the error?struct student *add(struct student *list,

char* name) {

struct student s;s.name = name;s.next = NULL;

if(list == NULL) {

return &s;

}else {

struct student *pos = list;

while(pos->next != NULL) {pos = pos->next;}

pos->next = &s;return list;}

}

Page 16: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Ok… so maybe we could receive the struct?struct student *add(struct student *list,

struct student s) {

if(list == NULL) {

return &s;

}else {

struct student *pos = list;

while(pos->next != NULL) {pos = pos->next;

}

pos->next = &s;return list;

}}

This is still an issue… Structs are passed by valueAnd parameters are also local variables…

Page 17: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Can we finally see one version that runs?struct student *add(struct student *list,

struct student *s) {

if(list == NULL) {

return s;

}else {

struct student *pos = list;

while(pos->next != NULL) {pos = pos->next;

}

pos->next = s;return list;

}}

Page 18: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Ok are we happy with add now?

• Nope. But, why?

• Right now to add a new student we need to do something like this:

struct student s = { .name = “Name”, .next = NULL};list = add(list, &s);

• What happens if the user can input the students?

• How many struct student do we need to allocate?

• We could potentially allocate an array so that we have structs to take from.

• The problem with that is: how many are enough? What happens if the user wants just one more than we gave them?

• It becomes very inneficcient.

• The solution: malloc!

Page 19: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

malloc (also in the Advanced Features Chapter)

• REALLY? Advanced features??? And pointer arithmetic is part of the pointer chapter??• Sorry I needed to complain about that decision the book author made.

• malloc is defined in stdlib.h • It stands for memory allocation.

• Malloc “dynamically” allocates memory

• It receives a single parameter of type size_t with the number of bytes to allocate• size_t is a custom type and it’s actual meaning is platform (computer)

dependent• Usually it is an unsigned long int (but not necessarily) • We’ve seen size_t before when we talked about the sizeof function.

• It returns a void* (a pointer to type void).• This is C’s “generic” pointer.• It can point to anything! Including code (recall my awkward discussion

about calling functions without a name)

Page 20: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

malloc

• So how do I use it?

int *ptr_int = (int*)malloc(4);

• Wait don’t use it like that….

• Even if we know that an int is 4 bytes for most of our uses….

• This ties our code to architectures that use 4 byte integers.

• The correct way of using it:

int *ptr_int = (int*)malloc(sizeof(int));

Page 21: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

malloc – Graphically explained

int *ptr_int;

ptr_int = (int*)malloc(sizeof(int));

?

Page 22: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

malloc – Graphically explained

int *ptr_int;

ptr_int = (int*)malloc(sizeof(int));

?

<int>

At this point this piece of memory isnot named. It is valid memory, but it’s not tiedto a variable name.

Page 23: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

malloc – Graphically explained

int *ptr_int;

ptr_int = (int*)malloc(sizeof(int));

?

<int>

Page 24: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

So how do we use it for our add function?struct student *add(struct student *list,

char* name) {

struct student s;s.name = name;s.next = NULL;

if(list == NULL) {

return &s;

}else {

struct student *pos = list;

while((*pos).next != NULL) {pos = (*pos).next;

}

(*pos).next = &s;return list;

}}

Page 25: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

So how do we use it for our add function?struct student *add(struct student *list,

char* name) {

struct student *s = (struct student*)malloc(sizeof(struct student));

s->name = name;s->next = NULL;

if(list == NULL) {

return s;

}else {

struct student *pos = list;

while((*pos).next != NULL) {pos = (*pos).next;

}

(*pos).next = s;return list;

}}

Page 26: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Delete function

• We will delete by index (same as we did before).

void delete(struct student *list,

int index) {

struct student *prev;struct student *current = list;

while(index > 0 ) {

index--;prev = current;current = current->next;

}

prev->next = current->next; }

Page 27: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Allowing deleting the head(We all want to get rid of Ana)

• Ok she is my best friend, so no we don’t…. But if we did... The current way doesn’t support that.

• We can do it recursively (and we also don’t need to take into account the previous! Which is cool!)

struct student *delete2(struct student *list,

int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next,

index-1);return list;

}}

Page 28: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Freeing memory

• We are dynamically asking for memory.

• Remember memory is a limited resource. • It is BIG for most of our applications. But it’s not infinite.

• Every time we call malloc, we are making a commitment to use that memory and eventually free it.

• Not freeing memory is bad…• It’s like punching your mom… like that bad…

• Ok maybe not THAT bad….

• When the program ends, all memory gets reclaimed• OS programmers are smart dudes…

• But we should always try to use memory responsibly

Page 29: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Freeing memory

• free is the function we use to free up memory we have created using malloc.

• free receives any pointer, it goes to that piece of memory and let’s the OS reclaim the memory.

• free(ptr_int);

Page 30: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Delete function

struct student *delete(struct student *list,

int index) {

struct student *prev;struct student *current = list;

while(index > 0 ) {

index--;prev = current;current = current->next;

}

prev->next = current->next;

free(current);

return list;}

Page 31: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Recursive delete

struct student *delete(struct student *list,

int index) {

if(index == 0) {

struct student *next = list->next;free(list);return next;

}else {list->next = delete(list->next, index-

1);return list;

}}

Why like that?

Page 32: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

PointersFRIDAY JULY, 10TH

Page 33: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative Stuff

• Quiz today.

• Quiz #5 grades were released yesterday.

• Homework #5 was posted.

• The solution to Homework #4 was also posted.

• Let’s check the homework specification.

Page 34: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Recursive delete

struct student *delete2(struct student *list,

int index) {

if(index == 0) {

struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next,

index-1);return list;

}}

Page 35: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Recursive delete

struct student *delete2(struct student *list,

int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next,

index-1);return list;

}}

Page 36: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Ana Andrew Cristina Juliana Michael

struct student *delete2(struct student *list,int index) {if(index == 0) {

return list->next;}else {

list->next = delete2(list->next, index-1);return list;

}}

list = delete2(list, 2);

list

Page 37: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2(list, 2)

Page 38: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)AnaAndre

wCristin

a Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Page 39: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)AnaAndre

wCristin

a Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Page 40: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)AnaAndre

wCristin

a Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Page 41: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)AnaAndre

wCristin

a Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Page 42: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)AnaAndre

wCristin

a Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Page 43: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 2-1)

Page 44: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Page 45: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Page 46: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Page 47: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Page 48: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 1-1)

Page 49: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 50: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 51: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 52: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 53: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 54: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

CristinaJuliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 55: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

CristinaJuliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 56: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

CristinaJuliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Juliana

Page 57: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

CristinaJuliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Juliana

Page 58: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Juliana

Page 59: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

Juliana

Page 60: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 61: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 62: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 63: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 64: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 65: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {return list->next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

Juliana

Page 66: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

list = delete2(list, 2);

Juliana

Page 67: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

Juliana

Page 68: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

void print(struct student *head) { while(head != NULL) {

printf("%s\n", (*head).name); head = (*head).next;

} }

Page 69: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

void print(struct student *head) { while(head != NULL) {

printf("%s\n", head->name); head = head->next;

} }

Page 70: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

void print(struct student *head) { while(head != NULL) {

printf("%s\n", head->name); head = head->next;

}} print(list);

Page 71: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

Ana

Andrew

Cristina

Juliana Michael

void print(struct student *head) { while(head != NULL) {

printf("%s\n", head->name); head = head->next;

}} print(list);

Cristina is no longer accessible from theHead (list). However, she is still in memory.

Page 72: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

Page 73: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

next

Page 74: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

next

Page 75: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

next

Page 76: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Cristina Juliana Micha

el

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

next

Page 77: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

delete2( , 0)

next

Page 78: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

list

delete2( , 2)Ana

Andrew

Juliana Michael

struct student *delete2(struct student *list,int index) {

if(index == 0) {struct student *next = list->next;free(list);return next;

}else {list->next = delete2(list->next, index-

1);return list;

}}

delete2( , 1)

next

Page 79: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Questions?

Page 80: Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Using malloc to create variable size arrays….