Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.

Similar presentations


Presentation on theme: "Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers."— Presentation transcript:

1 Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers

2 Addresses and Pointers

3 Address Operator A variable can be referenced using the address operator & example: scanf(“%f”, &x); This statement specifies that the value read is to be stored at the address of x

4 Pointer Assignment A pointer is a variable that holds the address of a memory location If a variable p holds the address of another variable q, then p is said to point to q If q is a variable at location 100 in memory, then p would have the value 100 (q’s address)

5 How to declare a pointer variable pointer variables are declared using an asterisk ( * ) The asterisk is called the indirection operator or the de-referencing operator). example: –int a, b, *ptr; ptr is a pointer to an integer when a pointer is defined, the type of variable to which it will point must be specified. (i.e. a pointer defined to point to an integer cannot also point to a floating point variable.)

6 Example int *iPtr; double* dPtr; the variable iPtr is declared to point to an int the variable dPtr is declared to point to a double neither variable has been initialized in the above example declaring a pointer creates a variable capable of holding an address

7 Example int a, *iPtr; char* s; double *dPtr; ? ? ? iPtr s dPtr - ? a

8 More about declaring pointers When using the form int* p, q; the * operator does not distribute. In the above example p is declared to be a pointer to int. q is declared to be an int.

9 Assigning values to a pointer the assignment operator (=) is defined for pointers the right operand can be any expression that evaluates to the same type as the left the operator & in front of an ordinary variable produces the address of that variable. The & operator is called to address of operator

10 Example example - int I=6, j; int *iPtr; iPtr = &I; j = *iPtr; 6 6 I j iPtr

11 Practice! Give a memory snapshot after each set of assignment statements int a=1, b=2, *ptr; ptr = &b; int a=1, b=2, *ptr=&b; a = *ptr;

12 NULL pointer A pointer can be assigned or compared to the integer zero, or, equivalently, to the symbolic constant NULL, which is defined in. A pointer variable whose value is NULL is not pointing to anything that can be accessed Is guaranteed to compare unequally with any pointer that is not a null pointer.

13 Example- int *iPtr=0; char *s=0; double *dPtr=NULL; iPtr s dPtr

14 Pointer Assignment A pointer can point to only one location at a time, but several pointers can point to the same location. Example /* Declare and initialize variables. */ int x=-5, y = 8, *ptr1, *ptr2; /* Assign both pointers to point to x. */ ptr1 = &x; ptr2 = ptr1; The memory snapshot after these statements are executed is -58 ptr1 ptr2 x y

15 Pointer Arithmetic Four arithmetic operations are supported –+, -, ++, -- –only integers may be used in these operations Arithmetic is performed relative to the variable type being pointed to Example:p++; –if p is defined as int *p, p will be incremented by 4 (system dependent) –if p is defined as double *p, p will be incremented by 8(system dependent –when applied to pointers, ++ means increment pointer to point to next value in memory

16 Comparing Pointers You may compare pointers using relational operators Common comparisons are: –check for null pointer (p == NULL) –check if two pointers are pointing to the same object (p == q) Is this equivalent to (*p == *q) –compare two pointers that are pointing to a common object such as an array.

17 Pointers and Arrays The name of an array is the address of the first elements (i.e. a pointer to the first element) The array name is a constant that always points to the first element of the array and its value can not be changed. Array names and pointers may often be used interchangeably. Example int num[4] = {1,2,3,4}, *p; p = num; /* above assignment is the same as p = &num[0]; */ printf(“%i”, *p); p++; printf(“%i”, *p);

18 More Pointers and Arrays You can also index a pointer using array notation Example: char string[] = “This is a string”; char *str; int i; str = string; for(i =0; str[i]; i++)//look for null printf(“%c”, str[i]);

19 Two-Dimensional Arrays A two-dimensional array is stored in sequential memory locations, in row order. Array definition:int s[2][3] = {{2,4,6}, {1,5,3}}, *sptr=&s[0][0]; Memory allocation: s[0][0]2 s[0][1]4 s[0][2]6 s[1][0]1 s[1][1]5 s[1][2]3 A pointer reference to s[0][1] would be *(sptr+1) A pointer reference to s[1][1] would be *(sptr+4) row offset * number of columns + column offset

20 Pointers in Function References In C, function references are call-by-value except when an array name is used as an argument. –An array name is the address of the first element –Values in an array can be modified by statements within a function To modify a function argument, a pointer to the argument must be passed The actual parameter that corresponds to a pointer argument must be an address or pointer.

21 switch Example void switch2(int *a, int *b) { /* Declare Variables. */ int temp; /* Switch values pointed to by a and b. */ temp = *a; *a=*b; *b=temp; /* Void return. */ return; }

22 Dynamic Memory Allocation Dynamically allocated memory is determined at runtime A program may create as many or as few variables as required, offering greater flexibility Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. Dynamic memory is finite Dynamically allocated memory may be freed during execution

23 Dynamic Memory Allocation Memory is allocated using the: –malloc function(memory allocation) –calloc function (cleared memory allocation) Memory is released using the: –free function The size of memory requested by malloc or calloc can be changed using the: –realloc function

24 malloc and calloc Both functions return a pointer to the newly allocated memory If memory can not be allocated, the value returned will be a NULL value The pointer returned by these functions is declared to be a void pointer A cast operator should be used with the returned pointer value to coerce it to the proper pointer type

25 Example of malloc and calloc int npts = 500; double *x; int *p; /* Allocate memory for 500 doubles. */ x = (double *)malloc(npts*sizeof(double)); /* Allocate memory for 500 integers. */ p = (int *)calloc(npts,sizeof(int)); Represents the number of bytes used to store a double value

26 #include int main() { int num_std,j; double ave=0.0,*grade; printf("How many students are there?\n"); scanf("%d",&num_std); //declare malloc grade=(double*)malloc(num_std*sizeof(double)); //declare calloc //grade=(double*)calloc(num_std,sizeof(double)); for(j=0;j<num_std;j++) printf("%lf\n",grade[j]); printf("Please enter the marks of %d students\n",num_std); for(int i=0;i<num_std;i++) { scanf("%lf",&grade[i]); ave+=grade[i]; } ave=ave/num_std; printf("The average is %.2lf \n",ave); free(grade); return 0; }


Download ppt "Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers."

Similar presentations


Ads by Google