C programming pointers
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

C programming---Pointers PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on
  • Presentation posted in: General

C programming---Pointers. The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into bytes. Each byte can hold 8 bits of information:. Each byte has a unique address in memory. Address. Address. Contents. 0. 1. 2. 3.

Download Presentation

C programming---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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


C programming pointers

C programming---Pointers

The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into bytes.

Each byte can hold 8 bits of information:

Each byte has a unique address in memory.


Address

Address

Address

Contents

0

1

2

3

n-1


Address1

Address

int i = 9;/* suppose sizeof(int) = 4

The address of variable i is 0xFA83

0xFA83

0xFA84

0xFA85

0xFA86


Pointer variables

Pointer Variables

Use a pointer variable p to store the address of a

variable i, and we say p “points to” i

0xFA83

0xFA84

0xFA85

0xFA86

p


Declaring pointer variables

Declaring Pointer Variables

int *p; // p is a pointer variable capable of pointing to objects of type int

char *str;

double *q;

C requires that every pointer variable point to objects of a particular type(the referenced type)

There are no restrictions on what referenced

type may be. In fact, a pointer variable can even point to another pointer.


The address and indirection operators

The Address and Indirection Operators

  • int i, *p;

  • …….

  • p = &i;

  • int i;

  • int *p = &i;

  • int i, p = &i;


The address and indirection operators1

The Address and Indirection Operators

printf(“%d\n”, *p);

j = *&i;


The address and indirection operators2

The Address and Indirection Operators

int i, *p = &i;

i = 1;

printf(“i = %d\n”, i);

printf(“*p = %d\n”, *p);

*p = 4;

printf(“i = %d\n”, i);

printf(“*p = %d\n”, *p);


Something to remember

Something to remember

Never apply the indirection operator to an

uninitialized pointer variable.

int *p;

printf(“%d”, *p);

Unless you know where a pointer points to, do

not make an assignment to the pointer


Pointer assignment

Pointer Assignment

int i, j, *p, *q;

p = *i;

q = p;

p

i

q


Pointer assignment1

Pointer Assignment

int i, j, *p, *q;

p = &i;

q = &j;

*q = *p;

p

i

q


Pointer as arguments

Pointer as Arguments

Example 2.c


Using const to protect arguments

Using const to Protect Arguments

When we call a function and pass it a pointer to a variable, we normally assume that the function will modify the variable.

Sometimes we just want to examine the value of a variable, not change it.

Using pointer might be efficient: time and memory space

void f(const int *p)

{

*p = 0; // wrong: p is a pointer to a “constant integer”

}


Pointers as return values

Pointers as Return Values

int *max(int *a, int *b)

{

if(*a > *b)

return a;

else

return b;

}

int *p = max(&a, &b);


Be careful

Be careful

int *f(void)

{

int a;

……

return &a;

}


Pointers and arrays

Pointers and Arrays

Pointer Arithmetic

int a[10], *p;

p = &a[0];

a

p


Pointer arithmetic

Pointer Arithmetic

p

q

p = &a[2];

q = p + 3;

p += 4;


Comparing pointers

Comparing Pointers

Using the relational operators( <, <=, >, >=) and the equality operator

(== and !=)

int *p = &a[5];

int *q = &a[1];

The value of p <= q is 0(false)

The value of p >= q is 1(true)


Using pointers for array processing

Using Pointers for Array Processing

#define N 10

int a[N], sum, *p;

sum = 0;

for(p = &a[0]; p < &a[N]; p++)

sum += *p;


Combining the and operators

Combining the * and ++ Operators

*p++ or *(p++)

(*p)++

*++p or *(++p)

++*p or ++(*p)

The postfix version of ++ takes precedence over *

See 3.c


Using an array name as a pointer

Using an Array Name as a Pointer

int a[10];

*a = 7; // modify a[0]

*(a + 2) = 13; // modify a[2]

while( *a != 0 )

a++; // wrong


Array arguments

Array Arguments

int find_largest(int a[], int n)

{

}

int find_largest(int *a, int n)

{

}


Using a pointer as an array name

Using a Pointer as an Array Name

#define N 10

…..

int a[N], i, sum = 0, *p = a;

…..

for(i =0; i < N; i++)

sum += p[i];


Pointers and multidimensional arrays

Pointers and Multidimensional Arrays

int a[NUM_ROWS][NUM_COLS];

int row, col;

for(row = 0; row < NUM_ROWS; row++)

for(row = 0; row < NUM_ROWS; row++)

a[row][col] = 0;

int *p;

for(p = &a[0][0]; p <= &a[NUM_ROWS-1][NUM_COLS]; p++)

*p = 0;


  • Login