slides5_05 - C Programming - Lecture 5 This lecture we will...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
C Programming - Lecture 5 This lecture we will learn: Why I harp on about pointers. Arrays of pointers. What are command line arguments. The realloc command. The difference between an array and a pointer. Why pointers are dangerous as well as confusing. All the rest of the C language.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What's the point of pointers? Pointers can be used as variable length arrays. Pointers can be used for advanced data structures. Pointers can be "cheaper" to pass around a program. You could program without using them but you would be making life more difficult for yourself. Some things simply can't be done sensibly in C without them.
Background image of page 2
Arrays of pointers More commonly used (by experienced programmers) is an array of pointers. We can use an array of pointers in a similar way to a multi-dimensional array. We can declare an array of pointers like so: char *name[]= {"Dave","Bert","Alf"}; /* Creates and initialises 3 names */ We can now use name[0] anywhere we could use a string.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example of pointer arrays int i; char *people[]= {"Alf","Bert","Charlie"}; for (i= 0; i < 3; i++} { printf ("String %d is %s\n",i+1,people[i]); } String 1 is Alf String 2 is Bert String 3 is Charlie Will print:
Background image of page 4
Example of pointer arrays (2) int *lists[100]; /* Get 100 ptrs to int */ int i; for (i= 0; i< 100; i++) { lists[i]= (int *)malloc(23*sizeof(int)); } /* Do something with them here */ for (i= 0; i < 100; i++) { free(lists[i]); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What are Command line arguments? In unix we can type, for example,
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/15/2011 for the course ECKOW 123 taught by Professor Riuyt during the Spring '11 term at University of Damascus.

Page1 / 18

slides5_05 - C Programming - Lecture 5 This lecture we will...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online