0% found this document useful (0 votes)
40 views

Scientific Programming and Computer Architecture 20

This document discusses arrays and pointers in the C programming language. It provides a simple C program that declares an integer array and integer pointer, assigns values to array elements and the pointer, and prints the addresses and values. The program output demonstrates that arrays decay to pointers when passed to functions, showing the addresses and values in hexadecimal. Later sections will cover passing arrays as arguments to functions using pointers and an example program implementing the Aitken iteration method.

Uploaded by

Mirsaeid Mouasvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Scientific Programming and Computer Architecture 20

This document discusses arrays and pointers in the C programming language. It provides a simple C program that declares an integer array and integer pointer, assigns values to array elements and the pointer, and prints the addresses and values. The program output demonstrates that arrays decay to pointers when passed to functions, showing the addresses and values in hexadecimal. Later sections will cover passing arrays as arguments to functions using pointers and an example program implementing the Aitken iteration method.

Uploaded by

Mirsaeid Mouasvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Scientific Programming and Computer Architecture

the operating system in a manner that is explained in a later chapter.


We began by noting that a sequence may be specified using three pieces of information: a pointer to the first
location, the size of each item, and the number of items. The definition int list[3] supplies all three pieces of
information. The address of the first location is the value of list. The size of each item is 4 bytes (on GNU/Linux)
because an int is four bytes. Finally, the number of items in the array is 3 as shown in figure 1.3↑. The first two
pieces of information are contained in the type and value of list. If list is passed as an argument to a function,
the length of the array, which is the third piece of information, must be supplied separately.
Machine or assembly languages too access data items in a sequence using an address, the size of each item,
and the index of the item, much as in C.
A C source with arrays and pointers
So far our discussion of arrays and pointers has been with reference to figure 1.3↑. We will now write a simple C
program illustrating the discussion.
Before looking at C source, a few comments about indentation are in order. For program source not meant
for display in a book, we use eight-space indentation. [10]  Tab stops are separated by eight characters and terminal
screens are conventionally 80 character wide. When the program code is lined up according to tab stops, the code is
much easier to browse. The nesting level of loops becomes readily evident. The nesting level is an indication of the
level of complexity of the code. Therefore, it is useful to be able to recognize the nesting level immediately. Too
many levels of nesting often imply that the code is poorly structured.
Let us look at the following code, which uses five-space indentation. The suggestion of eight-space
indentation assumes 80-character-wide lines. Here the lines are about 50 characters, and the indentation has been
scaled down proportionally.
1 #include <stdio.h>
2 int main()
3 {
4 int x;
5 int list[3];
6 int *a;
7 a = &x;
8 list[1]=2;
9 *a = 35+list[1];
10 printf("%p %p %d\n", &x, list, x);
11 }
We are allowed to say list[1] = 2; (line 8), but list = &x; would have been illegal. The reason is that
list has a value but is not the name of any location. On one run, this code had the following output (line 10).

0x7fff3cb8e414 0x7fff3cb8e400 37
The value of x is printed as 37, as we may have expected. The address of x and the value of list are
printed in hexadecimal as indicated by the 0x at front. Thus, each address is 48 bits long. Figure 1.3↑ may give the
impression that each address is the address of a location in physical memory. In fact, the addresses that are printed
out are virtual addresses, a concept we will discuss in later chapters.
The printf() function used on line 10 is part of the standard C library. Its declaration will be in the
standard header file stdio.h, which is included on line 1. The C compiler knows where to look for this header file.

1.2.3  The Aitken iteration using arrays and pointers

As already noted, arrays and pointers are almost equivalent in C. The principal advantage of thinking of arrays in
this way arises in passing arrays as arguments to functions. Here we use the Aitken example to illustrate how arrays
may be passed as pointers.
The file aitken.c begins with two directives :
#include <assert.h>
#include "aitken.h"

https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]

You might also like