Pointer arithmetic arrays only - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Pointer arithmetic arrays only

Description:

An argument count, and an array of argument values. int main(int argc, char *argv ... Can still use array notation, but also can argv and so on. sizeof ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 14
Provided by: michaelc7
Category:

less

Transcript and Presenter's Notes

Title: Pointer arithmetic arrays only


1
Pointer arithmetic arrays only
  • Can add or subtract an integer as long as
    result is still within the bounds of the array
  • Can subtract a pointer from another pointer iff
    both point to elements of the same array
  • char word cat
  • / create array of four chars cat\0 /
  • char p word / point p at first char /
  • while (p ! \0) / move pointer to end /
  • printf(word length d, p-word-1)
  • / subtract one address from another result is
    3 /
  • But no pointer multiplication or division, and
    cannot add two pointers

2
/ copy t to s /void stringcopy(char s, char
t)
  • One way to implement use subscript notation
  • int i 0
  • while ((si ti) ! \0) i
  • Another way use the pointer parameters
  • while ((s t) ! \0)
  • s t
  • Usually just increment in the while header
  • while ((s t) ! \0)
  • And its possible to be even more cryptic
  • while (s t) / Actually works! /

3
Multi-dimensional and pointer arrays, and
pointers to arrays
  • Multi-dimensional arrays arrays of arrays
  • int x53 / allocates memory for 15 ints /
  • Actually, 5 arrays, each able to store 3 integers
  • Arrays of pointers
  • int p5 / allocates memory for 5 pointers /
  • for (i0 ilt5 i) pi xi / x as above /
  • Now p can be used as an alias for x
  • Pointers to arrays require pointers to pointers
  • int px x / points to first array in x /
  • px / moves pointer to next array /

4
Command line arguments
  • Declare main with two parameters
  • An argument count, and an array of argument
    values
  • int main(int argc, char argv)
  • argc 1 plus the number of tokens typed by the
    user at the command line after the program name
  • argv0 is the program name
  • argv1argc-1 are the other tokens
  • Each one points to an array of characters (i.e.,
    a C string)
  • Note equivalent way to declare second parameter
  • char argv commonly used instead of above form
  • Can still use array notation, but also can argv
    and so on

5
sizeof
  • A unary operator computes the size, in bytes,
    of any object or type
  • Usage sizeof object or sizeof(type)
  • If x is an int, sizeof x sizeof(int) is true
  • Works for arrays too total bytes in whole array
  • Sometimes can use to find an arrays length
  • int size sizeof x / sizeof xi
  • Actual type of result is size_t
  • An unsigned integer defined in ltstddef.hgt
  • Similarly, diff_t is result type of pointer
    subtraction
  • Especially useful to find the sizes of structures

6
C structures
  • Structures are variables with multiple data
    fields
  • e.g., define structure to hold an int and a
    double
  • struct example
  • int x
  • double d
  • Create a structure, and assign a pointer to it
  • struct example e, ep e
  • Now can access fields by e or by ep
  • e.d 2.5 / use name and the dot . operator
    /
  • ep-gtx 7 / or use pointer-to-structure-field
    -gt operator /
  • Second way is short-cut version of (ep).x 7
  • Note sizeof e gt sizeof(int)sizeof(double)

7
typedef and macros
  • Can precede any declaration with typedef
  • Defines a name for the given type
  • typedef struct example ExampleType
  • ExampleType e, ep / e, ep same as prior
    slide /
  • Very handy for pointer types too
  • typedef ExampleType ETPointer
  • ETPointer ep / ep same as above /
  • Macros can simplify code too
  • define X(p) (p)-gtx
  • X(ep) 8 / preprocessor substitutes
    correct code /

8
Unions
  • Can hold different data types/sizes (at different
    times)
  • e.g., define union to hold an int or a double
  • union myValue
  • int x
  • double d
  • u, up / u is a union, up can point to one /
  • Access x or d by u. or up-gt just like structures
  • sizeof u is size of largest field in union
  • Equals sizeof(double) in this case
  • Often store inside a structure, with a key to
    identify type

9
C function memory notes
  • Parameters and local variables are automatic
  • i.e., they exist only while the function executes
  • So should never return a pointer to an automatic
    variable
  • Dynamic memory allocation is different (will
    discuss)
  • Variables always passed to functions by value
  • i.e., the value is copied, so functions operate
    on a copy
  • One issue is inefficient to pass structures
    pointers better
  • Another issue functions need pointers to change
    values
  • change(x) / xs value unchanged when function
    returns/
  • change(x) / function may have changed xs
    value /
  • Return values are copies too so similar issues

10
A parameter passing example
  • void triple1(int x) x x 3
  • void triple2(int x) x x 3
  • int a 10, 7
  • void main(void)
  • triple1(a0) / What is being passed? /
  • printf("d\n", a0) / What is printed? /
  • triple2(a) / What is being passed? /
  • printf("d\n", a0) / What is printed? /
  • Be sure to understand why these results occur.
  • Hint draw the memory storage including storage
    duration

11
Analogous example, re pointers
  • First, recall that pointers are variables too
    then
  • void repoint1(int p) p p 1
  • void repoint2(int p) p p 1
  • int a 10, 7
  • int ap a
  • void main(void)
  • repoint1(ap) / What is being passed? /
  • printf("d\n", ap) / What is printed? /
  • repoint2(ap) / What is being passed? /
  • printf("d\n", ap) / What is printed? /

12
2 ways to allocate memory
  • Static memory allocation done at compile-time
  • int x double a5 / space for 1 int, 5
    doubles /
  • Both size and type are clearly specified ahead of
    time x can only hold int values, a only doubles
  • Dynamic memory allocation during execution
  • Must use library methods like malloc
  • Allocates specific amount of memory, returns void
  • Cast to appropriate pointer type then use as
    always
  • int ip (int )malloc(sizeof(int))
  • Notes returns NULL if memory not available cast
    is optional
  • Must free the memory when done with it free(ip)
  • www.cs.stanford.edu/cslibrary/PointerFunC.avi

13
Maybe moreMaybe less
Write a Comment
User Comments (0)
About PowerShow.com