ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Published byModified over 4 years ago
Presentation on theme: "ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration."— Presentation transcript:
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
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
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 *)
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 an array of pointers or a pointer to an int array? What does (*(void(*)())0)() mean? (From Koenig: C Traps and Pitfalls, 1989)
Pointers and Arrays The name of the array is a pointer to the start of that array. – If a 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 (since *(a+3) is the same as *(3+a)).
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”;
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
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;
Examples of Declarations int **a; // ptr to ptr int *b; // array of ptrs int (*c); // ptr to array int *d(); // d returns ptr to int int (*e)(); // e is ptr to int function char (*(*x()))(); // ? char (*(*y)()); // ?? Kernighan & Ritchie, The C Programming Language, 1988
structs Keyword struct followed by an optional structure tag (or name) followed by struct members 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
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
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?
-> 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?