1 / 14

Pointers

Pointers. De La Salle University College of Engineering Electronics and Communications Engineering and Computer Engineering Department Computer Technology Fundamentals for Engineering Students (LBYEC72). Engr. Jose B. Lazaro Jr. Pointers

honey
Télécharger la présentation

Pointers

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. Pointers De La Salle University College of Engineering Electronics and Communications Engineering and Computer Engineering Department Computer Technology Fundamentals for Engineering Students (LBYEC72) Engr. Jose B. Lazaro Jr.

  2. Pointers • A pointer is a variable that contains an address - that is, it contains a reference to another location in memory that can contain a value. • You already used an address when you used the function scanf( ). • variables that can store addresses are called pointers. • int Number = 25; • int *pNumber = &Number;

  3. Contains the address of Number * Deference operator – its effect is to access the data stored at the address specified by a pointer.

  4. Declaring Pointers You can declare a pointer to a variable of type int with the following statement: int*pointer; The type of the variable with the name pointer is int *. It can store the address of any variable of type int. This statement just creates the pointer but doesn’t initialize it. Uninitialized pointers are particularly hazardous, so you should always initialize a pointer when you declare it. You can initialize pointer so that it doesn’t point to anything by rewriting the declaration like this: int*pointer = NULL;

  5. Declaring Pointers If you want to initialize your variable pointer with the address of a variable that you’ve already declared, you use the address of operator &: intnumber = 10; int*pointer = &number;

  6. Accessing a Value Through a Pointer You use the indirection operator,*, to access the value of the variable pointed to by a pointer. This operator is also referred to as the dereference operator because you use it to “dereference” a pointer. Suppose you declare the following variables: intnumber = 15; int*pointer = &number; intresult = 0; The pointer variable contains the address of the variable number, so you can use this in an expression to calculate a new value for total, like this: result = *pointer + 5;

  7. Using Pointers Because you can access the contents of number through the pointer pointer, you can use a dereferenced pointer in arithmetic statements. For example *pointer += 25; This statement increments the value of whatever variable pointer currently addresses by 25. The * indicates you’re accessing the contents of whatever the variable called pointer is pointing to. In this case, it’s the contents of the variable called number.

  8. Using Pointers The variable pointer can store the address of any variable of type int. This means you can change the variable that pointer points to by a statement such as this: pointer = &another_number;

  9. Using Pointers /* Program 1 What's the pointer */ #include <stdio.h> intmain(void) { long num1 = 0L; long num2 = 0L; long *pnum = NULL; pnum= &num1; /* Get address of num1 */ *pnum = 2; /* Set num1 to 2 */ ++num2; /* Increment num2 */ num2 += *pnum; /* Add num1 to num2 */ pnum= &num2; /* Get address of num2 */ ++*pnum; /* Increment num2 indirectly */ printf("\nnum1 = %ld num2 = %ld *pnum = %ld *pnum + num2 = %ld\n", num1, num2, *pnum, *pnum + num2); return 0; }

  10. Arrays and Pointers An array is a collection of objects of the same type that you can refer to using a single name. For example, an array called scores[50] could contain all your basketball scores for a 50-game season. You use a different index value to refer to each element in the array. scores[0] is your first score and scores[49] is your last. If you had ten games each month, you could use a multidimensional array, scores[12][10]. If you start play in January, the third game in June would be referenced by scores[5][2]. A pointer is a variable that has as its value the address of another variable or constant of a given type. You can use a pointer to access different variables at different times, as long as they’re all of the same type.

  11. Arrays and Pointers /* Program 2 Arrays and pointers - A simple program*/ #include <stdio.h> int main(void) { char multiple[] = "My string"; char *p = &multiple[0]; printf("\nThe address of the first array element : %p", p); p = multiple; printf("\nThe address obtained from the array name: %p\n", p); return 0; }

  12. Arrays and Pointers This program demonstrates the effect of adding an integer value to a pointer. /* Program 3 Arrays and pointers taken further */ #include <stdio.h> int main(void) { char multiple[] = "a string"; char *p = multiple; for(inti = 0 ; i<strlen(multiple) ; i++) printf("\nmultiple[%d] = %c *(p+%d) = %c &multiple[%d] = %p p+%d = %p", i, multiple[i], i, *(p+i), i, &multiple[i], i, p+i); return 0; }

  13. Laboratory Exercises : Exercise 1. Write a program to calculate the average for an arbitrary number of floating-point values that are entered from the keyboard. Store all values in memory that’s allocated dynamically before calculating and displaying the average. The user shouldn’t be required to specify in advance how many values there will be. Exercise 2. Write a program that will read an arbitrary number of proverbs from the keyboard and store them in memory that’s allocated at runtime. The program should then output the proverbs ordered by their length, starting with the shortest and ending with the longest. Exercise 3. Write a program that will read a string from the keyboard and display it after removing all spaces and punctuation characters. All operations should use pointers. Exercise 4. Write a program that will read a series of temperature recordings as floating-point values for an arbitrary number of days, in which six recordings are made per day. The temperature readings should be stored in an array that’s allocated dynamically and that’s the correct dimensions for the number of temperature values that are entered. Calculate the average temperature per day, and then output the recordings for each day together, with the average on a single line with one decimal place after the point.

More Related