Arrays and Pointers - Intro to Computer Programming - Lecture Slides, Slides of Computer Engineering and Programming

The key points in these lecture slides of intro to computer programming are given as:Arrays, Group of Items, One Variable Name, Same Data Type, One Dimensional Array, Thinking About Arrays, Declaring Arrays, Assigning Values to Arrays, Index, Number in Brackets, Program Example

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays and Pointers
Lecture 15
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Arrays and Pointers - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Arrays and Pointers

Lecture 15

Arrays and Pointers

  • So far, we have looked at pointer variables which were declared with statements like: FILE *fptr ; int *aptr ;
  • We have also used pointer constants when we sent the address of a variable to a function. We did this by placing an ampersand before the variable name. For instance, the address of variable a is &a, which is a pointer constant.

Arrays and Pointers

myptr = &myarray[2]; /* Assign the address of the third element of myarray to myptr */ printf(“%d”, myptr); / Print the value of what myptr is pointing to, i.e., the value of myarray[2] */

  • Note that the * in front of myptr de-references the pointer. That is, it says “use the value in the address that is pointed to.”
  • The following shows a small program and its output.

Arrays and Pointers

/* Printing array values and addresses */ #include <stdio.h> int main ( ) { int k; float a[4] = {1000, 2000, 3000, 4000}; printf ("k a &a[k] a[k]\n"); for (k=0; k<4; k++) printf ("%d %ld %ld %f\n", k, a, &a[k], a[k]); }

Arrays and Pointers

  • We have seen how to pass a pointer for a single valued variable to a function.
  • Sometimes we want to pass an entire array to a function. The name of an array without a subscript is a pointer constant that contains the address of element [ 0 ] of the array. Therefore, if we pass the array name with no subscript to a function, we are passing a pointer to that array.
  • The following program illustrates this.

Arrays and Pointers

/* Passing an array to a function */ #include <stdio.h> #include <string.h> void myfunct (int , char [ ]); int main ( ) { char name[20] = "Michael J. Miller"; myfunct (20, name); }

Arrays and Pointers

  • Given the declaration int a[3] = { 1, 2, 3 } ; a is a pointer to (the address of) a[0] &a[0] is a pointer to a[0] a[0] is the value 1 ( *a is also the value 1) &a[1] is a pointer to a[1] a[1] is the value 2 &a[2] is a pointer to a[2] a[2] is the value 3 &a[0]+1 is a pointer to a[1] &a[0]+2 is a pointer to a[2]

Arrays and Pointers

Given the declaration

int b[3][3] = {{1,3,5}, {7,9,11}, {13,15,17}};

b is a pointer to b[0][0] b[0] is also a pointer to b[0][0] b[1] is a pointer to b[1][0] b[2] is a pointer to b[2][0] *b is a pointer to b[0] (special case) *b[1] is the value of b[1][0] (which is 7) *b[2] + 1 is the value of b[2][0] + 1 Docsity.com

void printarray (int cal[][7],

int j)

{

int k, n ; for (k = 0 ; k < j ; k++) { for (n = 0 ; n < 7 ; n++) printf ("%3d ", cal[k][n]); Docsity.com

/* Double subscript arrays, user-written functions and pointer arithmetic */

#include <stdio.h>

void printarray (int * , int , int);

int main ( ) {

int calendar[5][7]= {{1,2,3,4,5,6,7}, {8, 9, 10,11,12,13,14},

Assignment G

  • A data file named g13.dat exists in the class "common area" on the UNIX system.
  • The file contains actual data from one test of an instrumented bicycle from an engineering hands-on lab experiment.
  • You should look at the file on the screen (with a "more" command), but DO NOT print it out, as it contains several thousand (but less than 12,000) lines of data. Docsity.com

Assignment G

  • You will note that at the beginning of the file

there are several lines of "header" information followed by many lines of data in four columns.

  • Count by hand the number of lines from the

beginning of the file until you get to a line that has the actual data in the four columns. (You will need this number in Step 2 later.)

Steps for G

  • For opening the data file and the output file,

just use the usual fopen routine.

  • For reading the header lines, it might be

rather convenient to read the complete line into a character array or string.

  • How long should this string be?
  • What routine might be used?

Steps for G

  • Input each of the lines of data arranged in the four columns, discarding the data values from each of the first three columns and storing only the data from the fourth column in a one-dimensional array. For skipping over the columns with unwanted data, you will need to use the assignment suppression operator, *, in the scanf format. Your program will need to detect the end-of-file (EOF) to know when to stop inputting data. Close the input file when you reach the EOF.