1 / 14

ECE 353: Lab C

ECE 353: Lab C. Pointers and Structs. Basics. A pointer holds an address to some variable Notation: Dereferencing operator: * i nt *x is a declaration that x is a pointer to a variable of type int x contains the location that the pointer is pointing to R eferencing operator: &

eara
Download Presentation

ECE 353: Lab C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 353: Lab C Pointers and Structs

  2. Basics • A pointer holds an address to some variable • Notation: • Dereferencing operator: * • int *x is a declaration that x is a pointer to a variable of type int • x contains the location that the pointer is pointing to • Referencing operator: & • & means “address of.” • If we declare int y, then &y is the address of y

  3. Given a definition of the form int *i • &i is the address of the pointer itself • i is the address this pointer points to • *i is the content of the address the pointer points to • Declaring a pointer does NOT automatically initialize it: be careful to avoid dangling pointers

  4. Pointer Arithmetic • Valid operations: • Assignment of pointers of the same type. • Addition or subtraction by an integer: all results are in units of the type to which the pointer points • Subtracting or comparing two pointers to members of the same array (the compiler is not obliged to warn you if they are not to the same array) • Zero assignment or comparison with zero • Illegal operations: • Adding two pointers • Multiplication, division, shifting, masking • Adding by a non-integer quantity • Assignment of pointers of different types without a cast (except for void *)

  5. Precedence Table • The precedence table specifies the order in which operations are carried out • Is the value of 3+4*4 equal to 28 or to 19? • Is the condition (a&b==1) different from ((a&b)==1)? • If we had declared i as a pointer to int, • Is *i+1 different from *(i+1)? • Is *i++ different from *i+1? • Is *i++ different from (*i)++? • Is *i++ different from ++*i? • Is *i() different from (*i)()? • What does 10**i mean? • If j is another pointer to int, what is *i-*j? • Is the declaration int *k[100] an array of pointers or a pointer to an int array? • What does (*(void(*)())0)() mean? (From Koenig: C Traps and Pitfalls, 1989)

  6. Pointers and Arrays • The name of the array is a pointer to the start of that array. • If a[10] is declared as an array of ints, a is a pointer to the first element of that array. • However, an array name is NOT a variable: you cannot assign a pointer to an array name or increment it with ++. • Because of the way array accesses are implemented, 3[a] means the same thing as a[3] (since *(a+3) is the same as *(3+a)).

  7. Strings • There is no string type built into the language • Strings are processed as character arrays • String literals are specified between double-quotes • These arrays are stored with a end-delimiter of \0. • Pointers are often used with strings • string.h should be included for access to the relevant library functions • Example: char *roomNumber; roomNumber = “ELAB 303”;

  8. Pointers to Pointers • int **p means that p is a pointer to a pointer to data of type int • There is a similarity between pointers to pointers and two-dimensional arrays • However, a pointer is NOT an array. • Storage: • Global & static variables: Data segment • Dynamic memory: Heap • Local variables: Stack

  9. const • const indicates to the compiler that you will not change the item so qualified. • Examples: • const int x; • int *const y; • const int *a; • const int *const b;

  10. Examples of Declarations • int **a; // ptr to ptr • int *b[10]; // array of ptrs • int (*c)[10]; // ptr to array • int *d(); // d returns ptr to int • int (*e)(); // e is ptr to int function • char (*(*x())[])(); // ? • char (*(*y[3])())[5]; // ?? Kernighan & Ritchie, The C Programming Language, 1988

  11. structs • Keyword struct followed by an optional structure tag (or name) followed by structmembers within braces. • A struct member can be the same as the tag: the context disambiguates them. • A struct member may itself be a struct • Reference to a member is via structure_name.member_name

  12. Legal Operations on structs • Copying • Assignment • Taking its address • Accessing its members • Passing to, and returning it from, a function call • Structure parameters are passed by value

  13. Pointers to structs • We can declare pointers to structs in the usual way. • If q is a struct type, then the declaration struct q *ptr; defines a pointer to this type • Question: If x is a field of struct type q, is (*ptr).x different from *ptr.x?

  14. -> notation • -> is shorthand: if ptr is a pointer to a structure of which x is a member, then ptr -> x refers to member x of the struct pointed to by ptr • The usual precedence conditions apply: • Is ++ptr->x the same as ++(ptr->x)? • Is ++ptr->x the same as (++ptr)->x?

More Related