0% found this document useful (0 votes)
71 views24 pages

Chapter 2

This document summarizes key programming concepts from Chapter 2, including arrays, pointers, and structures. Arrays allow storing a collection of related data elements and accessing them via indices. Pointers store the address of a variable in memory and can perform pointer arithmetic. Structures group related data types together to form records.

Uploaded by

Jenber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views24 pages

Chapter 2

This document summarizes key programming concepts from Chapter 2, including arrays, pointers, and structures. Arrays allow storing a collection of related data elements and accessing them via indices. Pointers store the address of a variable in memory and can perform pointer arithmetic. Structures group related data types together to form records.

Uploaded by

Jenber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 2

Programming Concepts
Chapter contents

• Arrays

• Pointers

• Structures

2
Arrays
• An array is a data structure which stores a fixed-size sequential
collection of elements of the same type.

• Instead of declaring individual variables, such as number0,


number1, ..., and number99, you declare one array variable such
as numbers and use numbers[0], numbers[1], and ..., numbers[99]
to represent individual variables.

• Declaring Arrays: type array_Name [ array Size ];

• The array_Size must be an integer constant greater than zero and


type can be any valid C++ data type.

3
Cont...
• You can initialize C++ array elements either one by one or using a
single statement as follows:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

• An element is accessed by indexing the array name. This is done


by placing the index of the element within square brackets after
the name of the array.
double salary = balance[9];
• Practice problem 2.1
4
Multi-dimensional Arrays

• C++ allows multidimensional arrays. Here is the general form of a


multidimensional array declaration:

type name[size1][size2]...[sizeN];

• The simplest form of the multidimensional array is the two-


dimensional array. A two-dimensional array is, in essence, a list of
one-dimensional arrays.

type arrayName [ x ][ y ];

• Multidimensional arrays may be initialized by specifying bracketed


values for each row.

5
Cont...

int a[3][4] = {
{0, 1, 2, 3} , /* initializes row indexed by 0 */
{4, 5, 6, 7} , /* initializes row indexed by 1 */
{8, 9, 10, 11} /* initializes row indexed by 2 */
};

• The following initialization is equivalent to previous example:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

• An element in 2-dimensional array is accessed by using the subscripts,


i.e., row index and column index of the array. For example:

int val = a[2][3];

• Practice problem 2.2


6
Pointers
• Every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand
(&) operator which denotes an address in memory.

• Practice problem 2.3

• A pointer is a variable whose value is the address of another


variable. Like any variable or constant, you must declare a pointer
before you can work with it.

• The general form of a pointer variable declaration is:


type *var-name;

7
Cont...
• Here, type is the pointer's base type; it must be a valid C++ type
and var-name is the name of the pointer variable.
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

• The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address.
• The only difference between pointers of different data types is the
data type of the variable or constant that the pointer points to.
• Practice problem 2.4
8
Pointers in C++
• Pointers have many but easy concepts and they are very
important to C++ programming.

• C++ Null Pointers:- C++ supports null pointer, which is a constant


with a value of zero defined in several standard libraries.

• C++ pointer arithmetic:- There are four arithmetic operators that


can be used on pointers: ++, --, +, -.

• C++ array of pointers:- You can define arrays to hold a number of


pointers.

• C++ pointer to pointer:- C++ allows you to have pointer on a


pointer and so on.

9
Cont...

• Passing pointers to functions:- Passing an argument by


reference or by address both enable the passed argument to
be changed in the calling function by the called function.

• Return pointer from functions C++ allows a function to


return a pointer to local variable, static variable and
dynamically allocated memory as well.

10
Null Pointers

• It is always a good practice to assign the pointer NULL to a pointer


variable in case you do not have exact address to be assigned. This
is done at the time of variable declaration.
• A pointer that is assigned NULL is called a null pointer.

• The NULL pointer is a constant with a value of zero defined in


several standard libraries, including iostream.
• Practice problem 2.5.

• On most of the operating systems, programs are not permitted to


access memory at address 0 because that memory is reserved by
the operating system.

11
Cont...
• However, the memory address 0 has special significance; it signals
that the pointer is not intended to point to an accessible memory
location.

• But by convention, if a pointer contains the null (zero) value, it is


assumed to point to nothing.

• Thus, if all unused pointers are given the null value and you avoid
the use of a null pointer, you can avoid the accidental misuse of an
uninitialized pointer.

• Many times, uninitialized variables hold some junk values and it


becomes difficult to debug the program.

12
Pointer Arithmetic
• As you understood pointer is an address which is a numeric value;
therefore, you can perform arithmetic operations on a pointer just
as you can a numeric value.
• There are four arithmetic operators that can be used on pointers:
++, --, +, and -.
• To understand pointer arithmetic, let us consider that ptr is an
integer pointer which points to the address 1000.
• Assuming 32-bit integers, let us perform the following arithmetic
operation on the pointer: ptr++ the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer.

13
Cont...
• This operation will move the pointer to next memory location
without impacting actual value at the memory location.

• If ptr points to a character whose address is 1000, then above


operation will point to the location 1001 because next character
will be available at 1001.

• We prefer using a pointer in our program instead of an array


because the variable pointer can be incremented, unlike the array
name which cannot be incremented because it is a constant
pointer.

• Practice problem 2.6 & 2.7.

14
Cont...
• Pointers may be compared by using relational operators, such as
==, <, and >.
• If p1 and p2 point to variables that are related to each other, such
as elements of the same array, then p1 and p2 can be
meaningfully compared.
• Practice problem 2.8.

• Pointers and arrays are strongly related. In fact, pointers and


arrays are interchangeable in many cases.
• For example, a pointer that points to the beginning of an array can
access that array by using either pointer arithmetic or array-style
indexing.

15
Cont...
• Practice problem 2.9.

• However, pointers and arrays are not completely interchangeable.

• It is perfectly acceptable to apply the pointer operator * to var but


it is illegal to modify var value.
• The reason for this is that var is a constant that points to the
beginning of an array and can not be used as l-value.
• Because an array name generates a pointer constant, it can still be
used in pointer-style expressions, as long as it is not modified.
• For example, the following is a valid statement that assigns var[2]
the value 500: *(var + 2) = 500; Practice problem 2.10

16
Array of Pointers

• There may be a situation, when we want to maintain an array,


which can store pointers to an int or char or any other data type
available.

• Following is the declaration of an array of pointers to an integer:

int *ptr[MAX];
• This declares ptr as an array of MAX integer pointers. Thus, each
element in ptr, now holds a pointer to an int value.

• Practice problem 2.11.

17
Cont...
• You can also use an array of pointers to character to store a list of
strings.
• Practice problem 2.12.

• Normally, a pointer contains the address of a variable. When we


define a pointer to a pointer, the first pointer contains the address
of the second pointer, which points to the location that contains
the actual value.
• Following is the declaration to declare a pointer to a pointer of
type int: int **var;
• Practice problem 2.13, 2.14, and 2.15.

18
Structures
• Structure is a user defined data type which allows to combine
data items of different kinds.
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];

• The structure tag is optional and each member definition is a


normal variable definition.
• At the end of the structure's definition you can specify one or
more structure variables but it is optional.
19
Cont...
• To access any member of a structure, we use the member access
operator (.).
• The member access operator is coded as a period between the
structure variable name and the structure member that we wish
to access.
• You would use struct keyword to define variables of structure
type.
• Practice problem 2.17.
• You can pass a structure as a function argument in very similar
way as you pass any other variable or pointer.
• Practice problem 2.18.

20
Cont...
• You can define pointers to structures in very similar way as you
define pointer to any other variable as follows:

struct Books *struct_pointer;


• Now, you can store the address of a structure variable in the
above defined pointer variable.
• To find the address of a structure variable, place the ‘&’ operator
before the structure's name as follows:

struct_pointer = &Book1;
• To access the members of a structure using a pointer to that
structure, you must use the -> operator as follows:
struct_pointer->title;
21
Cont...
• Practice problem 2.19.

• There is an easier way to define structs or you could "alias" types


you create.
typedef struct
{
char title[50];
char author[50];
char subject[100];
int book_id;
}Books;

22
Cont...

• Now, you can use Books directly to define variables of Books type
without using struct keyword. Following is the example:

Books Book1, Book2;


• You can use typedef keyword for non-structs as well as follows:
typedef long int *pint32;
pint32 x, y, z;
• x, y and z are all pointers to long ints.
• Practice problem 2.20.

23
Thank You!!!

24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy