PHYS2020 Programming. Pointers in C

PHYS2020 Programming Pointers Pointers in C •  The thing that sets the C language apart from other programming languages is the use of pointers. •  ...
Author: Victor Powers
1 downloads 2 Views 836KB Size
PHYS2020 Programming

Pointers

Pointers in C •  The thing that sets the C language apart from other programming languages is the use of pointers. •  Pointers are a type of variable. •  What do pointers do?

1

Pointers Point! But what does the pointer point to?

What does the pointer point to?

2

You decide!

• The same pointer can point to different variables! • As long as they are of the correct type.

Pointers Point!

3

What do Pointers point to? Address 0022FF87 0022FF88 0022FF89 0022FF8A 0022FF8C 0022FF8D 0022FF8E

Value Char c int i float f

What do Pointers point to? •  Pointers “point” to the address in memory where a variable is stored. •  When you declare a variable, the compiler sets aside a location in memory, of the appropriate size for your variable (which depends on the type).

4

How much memory is set aside? •  You can use the “sizeof()” built-in C operator to find out. •  eg: •  int my_int –  printf(“The size of the int variable my_int =%d \n”,sizeof(my_int));

•  It actually depends on the variable type: see program next slide.

/* A program to print out various machine-dependent constants */ /* Michael Ashley / UNSW / 03-Mar-2003 */ #include #include #include

/* for printf definition */ /* for CHAR_MIN, CHAR_MAX, etc */ /* for FLT_DIG, DBL_DIG, etc */

int main(void) { printf("char %d bytes %d to %d \n", sizeof(char ), CHAR_MIN, CHAR_MAX ); printf("unsigned char %d bytes %d to %d \n", sizeof(unsigned char ), 0 , UCHAR_MAX ); printf("short %d bytes %hi to %hi \n", sizeof(short ), SHRT_MIN, SHRT_MAX ); printf("unsigned short %d bytes %hu to %hu \n", sizeof(unsigned short), 0 , USHRT_MAX ); printf("int %d bytes %i to %i \n", sizeof(int ), INT_MIN , INT_MAX ); printf("unsigned int %d bytes %u to %u \n", sizeof(unsigned int ), 0 , UINT_MAX ); printf("long %d bytes %li to %li \n", sizeof(long ), LONG_MIN, LONG_MAX ); printf("unsigned long %d bytes %lu to %lu \n", sizeof(unsigned long ), 0 , ULONG_MAX ); printf("float %d bytes %e to %e \n", sizeof(float ), FLT_MIN , FLT_MAX ); printf("double %d bytes %e to %e \n", sizeof(double ), DBL_MIN , DBL_MAX ); printf("precision of float %d digits\n", FLT_DIG); printf("precision of double %d digits\n", DBL_DIG); return 0; }

5

NaN: Not a Number • There are some redundant bit patterns in the representation of floating-point numbers: e.g., 0.0 with any exponent is still zero. • One of these bit patterns is used as a flag to indicate that a calculation produced an illegal result. • This bit pattern is called "NaN" which stands for "not a number". E.g, if you take the square root of a negative number, you will receive the result NaN. • Any operation on a NaN will continue to produce a NaN (e.g., if you subtract two NaNs you will still have a NaN). • This is useful to propagate such error conditions in your programs so that you are aware

How do I find the Address of a variable? •  Remember the unary operator “&”? •  This operator returns the address of the variable to the right of it: •  e.g. •  int my_int –  printf(“The address of the int variable my_int=%p \n”,&my_int);

•  The %p is the pointer placeholder, and can display a hexadecimal address

6

Pointers •  A pointer is a variable that holds a memory address. •  This is different to the value stored at the address. •  C is different because it has the ability to manipulate addresses. •  The makes it more powerful, but also more complex. • 

Pointers •  You can only write very simple programs in C if you don't understand pointers, even if you choose not to use them, except when necessary. •  This is because sometimes C wants to know the variable to manipulate it, and other times it wants to know the address. •  You need to know which is required and why. (e.g. scanf(“Enter a number\n”, &my_num);

7

Declaring a pointer •  type *my_pointer –  where type is int, char, float etc. •  Even though the pointer specifies the address, it still needs to know the type of variable to know how many spaces in memory are set aside. •  Not surprisingly, the type of pointer must match the type of variable stored at the address. •  The pointer must point to a valid variable, declared explicitly (as we have done in this course), or implicitly. •  See example program “pointerexample1.c” for examples of both usages.

Using pointers •  The rules can be complex, and you need to learn a few situations, but when using a pointer variable you: •  1. Declare it (use an asterisk) •  2. initialise it (don't use an asterisk) •  3. Use it! Once declared (int m; int *p), used in the program, p refers to the bytes with the address, *p refers to the bytes of the variable p points to, which is whatever we specify in the program. •  4. See programs pointerexample1.c and pointerexample2.c for more information.

8

Stuff not related to pointers

More stuff not related to pointers

9