Array Pointers

Embed Size (px)

Citation preview

  • 8/8/2019 Array Pointers

    1/57

    Copyright : Nahrstedt, Angrave, Abdelzaher 1

    C Survival Guide

    Lawrence Angrave

  • 8/8/2019 Array Pointers

    2/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    2

    C in 3 slides: Pointers& 'address-of ' (reference operator)

    * 'contents-of ' (dereference operator) Automatic variables are temporary and storedin the stackchar* p; p is a point er to a charac t er.

    *p =0; c ont ents-of p s e t to 0. (Kab oo m!) After declaring a pointer, initialize it tosomething before using it. (Doh!)

  • 8/8/2019 Array Pointers

    3/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    3

    Instant C #2: StringsC strings are terminated with a null byte

    strcpy( "hello", "world" ) will crashstrcmp(s1,s2) returns 0 if sameargv[0] is the program name

    argv[argc] is a null pointer

  • 8/8/2019 Array Pointers

    4/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    4

    Instant C #3: Heap Allocationm alloc( bytes ) to reserve heap memorylater f ree(ptr)

    static char * ptr;static variables are not stored in stack

  • 8/8/2019 Array Pointers

    5/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    5

    Common Causes of 'Death '*Uninitialized pointers

    strcpy(dest,"hello");

    C Strings need null byte at the endBuffer overflowUn-initialized memoryToo confident: not checking returnvaluesMiss-use of static vs. stack variables.

    *Talk like a Professional : "Segfault" / "Signal 11"

  • 8/8/2019 Array Pointers

    6/57

    R eview1. in t p 1 ;

    What does &p1 mean?

    2. char**argv ;What type is argv ?What type is *argv ?What type is **argv ?

    3 . ma in ( in t argc,char**argv) {what is *argv ?what is argv[argc ] ?

    4. What are the differences between x

    and y ?char* f () {char *x;stat i c char*y;retur n y;

    }

    Fix the following code if necessary

    1. if (strc m p("a","a"))

    pr in t f ("sa m e!");

    2. in t i =4;in t * i ptr;i ptr = & i ;* i ptr = 5;// n ow i =5

    3 . char *p;p=(char*) m alloc(99);strcpy("Hello",p);pr in t f ("%s World",p);f ree(p);

    4 . char m sg[5];

    strcpy ( m sg,"Hello");

  • 8/8/2019 Array Pointers

    7/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    7

    Writing C code is easy!void* myfunction() {

    char *p;*p = 0;return (void *) &p;

    Good news: You've used C++ in CS225 so

  • 8/8/2019 Array Pointers

    8/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    8

    Writing C code is easy !void* myfunction() {

    char *p;*p = 0;return (void *) &p;

    that crashes / is unpredictable

  • 8/8/2019 Array Pointers

    9/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    9

    How do I write good C

    programs?Fluency in C syntax

    Stack vs. HeapKey skill: read code for bugs(compiler warnings,if any, and testing areinsufficient)

  • 8/8/2019 Array Pointers

    10/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    10

    100 minute challengePointersC character arrays & string functionsStack & heap allocationTools:make, gcc, (gdb)

    Exchange rate: 10 minutes learning = 100 minutes of debugging an MP

  • 8/8/2019 Array Pointers

    11/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    11

    C is powerful - it 's the System

    Programmer 's choice language

    Sun Operating System

    Linux Operating SystemBSD & Apple OS X

    R uby, Python, JVM, gccbash shell, dir

    Apache web serverhttp://svn.apache.org/repos/asf/httpd/httpd/trunk/server/util.c

    http://www.google.com/codesearch?q=lang:c

  • 8/8/2019 Array Pointers

    12/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    12

    The C Language Spirit Made by professional programmers for professionalprogrammers

    Very flexible, very efficient and portableDoes not protect the programmers from themselves.R ationale: programmers know what they are doing.

    UNIX and most serious system software (servers,compilers, etc) are written in C.Can do everything Java and C++ can. It ll just lookuglier in C

  • 8/8/2019 Array Pointers

    13/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    13

    Compilergcc

    PreprocessorCompilerLinkerSee manual man for options: man gcc

    "Ansi-C" standards C89 versus C99C99: Mix variable declarations and code (for int i= )C++ inline comments //a comment

    make a compilation utilityGoogle 'makefile '

  • 8/8/2019 Array Pointers

    14/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    14

    Programming in CC = Variables + Instructions

  • 8/8/2019 Array Pointers

    15/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    15

    Programming in CC = Variables + Instructions

    intchar

    float

    string

    pointer array

  • 8/8/2019 Array Pointers

    16/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    16

    Programming in CC = Variables + Instructions

    intchar

    float

    string

    pointer array

    printf/scanf assignment

    if

    switch

    for while

  • 8/8/2019 Array Pointers

    17/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    17

    10,000

    10,002

    10,008

    10,010

    10,012

    Variables

    Value1

    Value2

    Value3

    Value4

    Value5

    x

    y

    zp

    d

    Memory Address

    Name

    Value

  • 8/8/2019 Array Pointers

    18/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    18

    10,000

    10,002

    10,008

    10,010

    10,012

    The & Operator:

    R eads Address of

    Value1

    Value2

    Value3

    Value4

    Value5

    x

    y

    zp

    d

    Name

    Value

    &y

  • 8/8/2019 Array Pointers

    19/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    19

    10,000

    10,002

    10,008

    10,010

    10,012

    Pointers

    Value1

    Value2

    Value3

    10,002

    Value5

    x

    y

    zp

    d

    Name

    Value

    A pointer is a variablewhose value is the

    address of another

  • 8/8/2019 Array Pointers

    20/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    20

    10,000

    10,002

    10,008

    10,010

    10,012

    The * Operator

    R eads Variable pointed to by

    Value1

    Value2

    Value3

    10,002

    Value5

    x

    y

    zp

    d

    Name

    Value

    A pointer is a variablewhose value is the

    address of another*P

  • 8/8/2019 Array Pointers

    21/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    21

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

  • 8/8/2019 Array Pointers

    22/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    22

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

    #@ *%!

    #@%$!

    @*%^

    p

    q

    x

  • 8/8/2019 Array Pointers

    23/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    23

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

    #@ *%!

    #@%$!

    10

    p

    q

    x

  • 8/8/2019 Array Pointers

    24/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    24

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

    #@%$!

    10

    p

    q

    x

  • 8/8/2019 Array Pointers

    25/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    25

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

    #@%$!

    11

    p

    q

    x

  • 8/8/2019 Array Pointers

    26/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    26

    What is the Output?main() {

    int *p, q, x;x=10;p=&x;*p=x+1;

    q=x;printf ( Q = %d\n , q);

    }

    11

    11

    p

    q

    x

  • 8/8/2019 Array Pointers

    27/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    27

    Cardinal R ule: Must Initialize

    Pointers before Using themint *p;*p = 10;

    BAD

  • 8/8/2019 Array Pointers

    28/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    28

    Cardinal R ule: Must Initialize

    Pointers before Using themint *p;*p = 10;

    #@ *%!p??

    Pointing somewhererandom

  • 8/8/2019 Array Pointers

    29/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    29

    Cardinal R ule: Must Initialize

    Pointers before Using themint *p;*p = 10;

    #@ *%!p

    #@ *%!10

  • 8/8/2019 Array Pointers

    30/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    30

    How to Initialize Pointers

  • 8/8/2019 Array Pointers

    31/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    31

    How to Initialize PointersSet pointer equal to location of knownvariable

    int *p;int x;

    p=&x;

  • 8/8/2019 Array Pointers

    32/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    32

    How to Initialize PointersUse malloc()

    int *p;

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

  • 8/8/2019 Array Pointers

    33/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    33

    How to Initialize PointersCreate an Array

    int p[10];

    Same as:int *p;p=(int *) malloc (10 *sizeof (int));

  • 8/8/2019 Array Pointers

    34/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    34

    Arraysint p[5];

    p[0]p[1]p[2]p[3]p[4]

    Name of array (is a pointer)

    p

    Shorthand:*(p+1) is called p[1]*(p+2) is called p[2]etc..

  • 8/8/2019 Array Pointers

    35/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    35

    Exampleint y[4];y[1]=6;y[2]=2;

    6

    2

    y[0]y[1]y[2]y[3]

    y

  • 8/8/2019 Array Pointers

    36/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    36

    Array Name as PointerWhat s the difference between the examples below:

    Example 1:int z[8];int *q;q=z;

    Example 2:int z[8];int *q;q=&z[0];

  • 8/8/2019 Array Pointers

    37/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    37

    Array Name as PointerWhat s the difference between the examples below:

    Example 1:int z[8];int *q;q=z;

    Example 2:int z[8];int *q;q=&z[0];

    NOTHING!!

    x (the array name) is a po intert o the beginning of the array , which is & x[ 0]

  • 8/8/2019 Array Pointers

    38/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    38

    Example:How much is y at the end:int y, x, *p;

    x = 20;

    *p = 10;y = x + *p;

  • 8/8/2019 Array Pointers

    39/57

  • 8/8/2019 Array Pointers

    40/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    40

    Question:What s the difference between

    int* q;int q[5];

    What s wrong with:

    int ptr[2];ptr[1] = 1;ptr[2] = 2;

  • 8/8/2019 Array Pointers

    41/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    41

    Question:What is the value of b[2] at the end?

    int b[3];int* q;

    b[0]=48; b[1]=113; b[2]=1;q=b;*(q+1)=2;b[2]=*bb[2]=b[2]+b[1];

  • 8/8/2019 Array Pointers

    42/57

    C i h A Abd l h N h d

  • 8/8/2019 Array Pointers

    43/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    43

    ConventionsStrings

    string c

    Character c

    C i ht A Abd l h N h t dt

  • 8/8/2019 Array Pointers

    44/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    44

    String Operationsstrcpystrlenstrcat strcmp

    Cop right : Angra e Abdel aher Nahrstedt

  • 8/8/2019 Array Pointers

    45/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    45

    strcpy, strlenSyntax:strcpy(ptr1, ptr2);

    where ptr1 and ptr2 are pointers to charvalue = strlen(ptr);

    where value is an integer andptr is a pointer to char

    Example:int len;char str[15];strcpy (str, "Hello, world!");

    len = strlen(str);

    Copyright : Angrave Abdelzaher Nahrstedt

  • 8/8/2019 Array Pointers

    46/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    46

    strcpy, strlenWhat s wrong with

    char str[5];

    strcpy (str, "Hello");

    Copyright : Angrave Abdelzaher Nahrstedt

  • 8/8/2019 Array Pointers

    47/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    47

    strncpySyntax:strncpy(ptr1, ptr2, num);

    where ptr1 and ptr2 are pointers to charnum is the number of characters to be copied

    Example:int len;char str1[15], str2[15];strcpy (str1, "Hello, world!");

    strncpy (str2, str1, 5);

    Copyright : Angrave Abdelzaher Nahrstedt

  • 8/8/2019 Array Pointers

    48/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    48

    strncpySyntax:strncpy(ptr1, ptr2, num);

    where ptr1 and ptr2 are pointers to charnum is the number of characters to be copied

    Example:int len;char str1[15], str2[15];strcpy (str1, "Hello, world!");

    strncpy (str2, str1, 5);

    Caution: strncpy blindly copies thecharacters. It does not voluntarily appendthe string-terminating null character.

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    49/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

    49

    strcat Syntax: strcat(ptr1, ptr2);

    where ptr1 and ptr2 are pointers to char

    Concatenates the two null terminates stringsyielding one string (pointed to by ptr1).

    char S[25] = "world!";char D[25] = "Hello, ";strcat(D, S);

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    50/57

    Copy g t : g ave, bde a e , a stedt

    50

    strcat Example

    What s wrong with:

    char S[25] = "world!";strcat(Hello, , S);

  • 8/8/2019 Array Pointers

    51/57

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    52/57

    py g g

    52

    Math: Increment and

    Decrement OperatorsExample 1:int x, y, z, w;

    y=10; w=2;x=++y;z=--w;

    Example 2:int x, y;y=10; w=2;x=y++;

    z=w--;

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    53/57

    53

    Math: Increment and

    Decrement OperatorsExample 1:int x, y, z, w;

    y=10; w=2;x=++y;z=--w;

    Example 2:int x, y;y=10; w=2;x=y++;

    z=w--;

    First increment/decrement then assign result x is11, z is 1

    First assign then increment/decrement x is 10, z is 2

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    54/57

    54

    Math: Increment and Decrement

    Operators on PointersExample 1:int a[2];int number1, number2, *p;a[0]=1; a[1]=10; a[2]=100;p=a;number1 = *p++;number2 = *p;

    What will number1 and number2 be at the end?

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    55/57

    55

    Math: Increment and Decrement

    Operators on PointersExample 1:int a[2];int number1, number2, *p;a[0]=1; a[1]=10; a[2]=100;p=a;number1 = *p++;number2 = *p;

    What will number1 and number2 be at the end?

    Hint: ++ increments pointer p not variable *p

    Copyright : Angrave, Abdelzaher,Nahrstedt

  • 8/8/2019 Array Pointers

    56/57

    56

    Logic: R elational (Condition)

    Operators== equal to!= not equal to> greater than< less than

    >= greater than or equal to

  • 8/8/2019 Array Pointers

    57/57

    Logic Exampleif (a == b) printf (Equal.);else printf (Not Equal.);

    Question: what will happen if I replaced the above

    with:if (a = b) printf (Equal.);else printf (Not Equal.);