您的位置:首页 > 编程语言 > C语言/C++

指针C语言-很好的教程

2014-12-01 22:53 337 查看
Summary: this tutorial introduces you to C pointer, which is an important concept in C programming language. Pointers give you a flexible and powerful way of manipulating data in your programs.

Introduction to C Pointer

When you define a
variable in your program, the compiler allocates a memory location with a unique address to store that variable’s value. You then access the memory address through the variable name.

For example, when you define a variable:

intx= 10;

1

int
x=
10;

You specified variable name (
x
), its data type
int
and its value,
10
. The variable
x
resides in the memory with a unique memory address. To get the memory address of the variable
x
, you use
unary operator
&
as follows:

printf("The memory address of xis %p\n",&x);

1

printf("The memory address of xis %p\n",&x);

In our system, the memory address of xis:

The memory address of xis 0028FF1C

1

The
memory address
of x
is 0028FF1C

Because memory address of
x
is a number (
28FF1C
), you can use another variable to store it e.g.,
px
as the following picture:



In C, we say that
px
points to x, or
px
is a pointer to
x
.



By definition, C pointer is a special variable that holds the memory address of another variable.

There is a special kind of pointer called
function pointer that holds a memory address of a
function. Check it out the
C function pointer tutorial for more information.

Declaring a pointer

Like a variable, before using a pointer, you have to declare it. The following illustrates the syntaxof declaring a pointer:

<type> *<pointer>;

1

<type>
*<pointer>;

First, you specify the data type of the variable that the pointer pointto. The type can be any valid

C data type such as
int
,
char
,
float
or even
void
.

Second, you place the indirection operator (
*
) in front of the pointer name to indicate that this is a pointer that points to a variable with a specific type e.g.
int
, not a variable of the type
int


Third, you provide the name for the pointer. The name of pointers must follow the naming rules of

variables. By convention, in C, pointer name begins with
p
to help distinguish between a pointer and a variable in your programs.

The following example defines 2 pointers that pointto
int
variables, an a pointer that points to a
char
variable.

int*px, *py;
char*ps;

1
2

int
*px,
*py;

char*ps;

Initializing Pointers

If you declare a pointer without initializing it, you have an uninitialized pointer. With an uninitialized pointer, you cannot do anything with it.

To initialize a pointer, you assign the memory address of another variable to the pointer using the address-of operator (
&
) as follows:

pointer = &variable;

1

pointer
= &variable;

For example, to assign the address of the
x
variable to the pxpointer you use the following syntax:

px= &x;

1

px
= &x;

Using pointers

After initializing a pointer, you can manipulate the variable which the pointer points to using the indirection operator (
*
).

For example, we can display the value of xthrough the
px
pointer as follows:

printf("%d",*px);

1

printf("%d",*px);

We can change the value of
x
through the
px
pointer as well:

*px= 20;
printf("*px= %d\n",*px); /* 20 */
printf("x= %d\n",x); /* 20 */

1
2
3

*px
= 20;

printf("*px= %d\n",*px);
/* 20 */
printf("x= %d\n",x);
/* 20 */

In C, accessing the value of a variable through the variable name is called
direct access, and accessing the value of a variable through a pointer that points to the variable is known as
indirect access or indirection.

Putting it all together.

/*
* File: main.c
* Author: zentut.com
* C Pointers Demo
*/
#include <stdio.h>

intmain()
{
intx= 10;
int* px= &x;

printf("Direct access, x= %d\n",x);
printf("Indirect access, *px= %d\n",*px);

*px= 20;

printf("Direct access, x= %d\n",x);
printf("Indirect access, *px= %d\n",*px);

/* examining memory address of xand px*/
printf("The memory address of xis %p\n",&x);
printf("The value of the pointer pxis %p\n",px);
printf("The memory address of the pointer pxis %p\n",&px);

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

/*

* File: main.c
* Author: zentut.com

* C Pointers Demo
*/

#include <stdio.h>


intmain()
{

int
x=
10;
int*
px=
&x;


printf("Direct access, x= %d\n",x);

printf("Indirect access, *px= %d\n",*px);


*px
= 20;


printf("Direct access, x= %d\n",x);
printf("Indirect access, *px= %d\n",*px);


/* examining memory address of xand px*/

printf("The memory address of xis %p\n",&x);
printf("The value of the pointer pxis %p\n",px);

printf("The memory address of the pointer pxis %p\n",&px);


return
0;
}

The following is the output of the program:

Direct access, x= 10
Indirect access, *px= 10
Direct access, x= 20
Indirect access, *px= 20
The memory address of xis 0028FF1C
The value of the pointer pxis 0028FF1C
The memory address of the pointer pxis 0028FF18

1
2
3
4
5
6
7

Directaccess,
x=
10

Indirect access,
*px
= 10
Directaccess,
x=
20

Indirect access,
*px
= 20
The
memory address
of x
is 0028FF1C

The value of
the pointer px
is 0028FF1C
The
memory address
of the pointer
pxis
0028FF18

Pointer & Array

Basically any operation on an array, which can be done using array subscript, can be achieved through a pointer to the array.

Let’s take a look at an example.

inta[10];

1

inta[10];

This declares an array of 10 integers. Underneath, there is a block of memory with 10 consecutive objects defining as follows:



The
a[i]
refers the ith element of the
a
array. The following defines a pointer that points to the first element of the
a
array:

int* pa;
pa = &a[0];

1
2

int
* pa;

pa =
&a[0];

Because the
pa
pointer points to the first element of the
a
array,
*pa
is the value of the first element. And
(pa+1)
points to the next element of the array, and
(pa+i)
points to the ith element.



Recall that the name of an array is the memory address of the first element, therefore the following assignment:

pa = &a[0];

1

pa
= &a[0];

can be rewritten as follows:

pa = a;

1

pa
= a;

After assigning the pointer to the first element of an array, you can perform any operations on that array. The following example illustrates how to manipulate an array via a pointer.

/*
* File : main.c
* Author : zentut.com
* Purpose: pointer to an array demo
*/
#include <stdio.h>

#define MAX 10

void display_array(int*,const int);

intmain()
{
inti;
inta[MAX];
int* pa;
pa = a;

/* fill array with random number between 0 and 100 */
for(i = 0; i < MAX; i++)
{
*(pa + i) = rand() % 100;
}
/* display array via pointer */
display_array(pa,MAX);

/* display array via array name */
display_array(a,MAX);

return 0;
}

/*
Display an array of integers with predefined size
*/
void display_array(int* p,const intsize)
{
inti;
for(i = 0; i < size; i++)
{
printf("%d ",p[i]);
}
printf("\n");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

/*

* File : main.c
* Author : zentut.com

* Purpose: pointer to an array demo
*/

#include <stdio.h>


#define MAX 10


void display_array(int*,const
int);


intmain()
{

int
i;
inta[MAX];

int*
pa;
pa
= a;


/* fill array with random number between 0 and 100 */

for(i
= 0;
i <
MAX;
i++)
{

*(pa
+ i)
= rand()
% 100;
}

/* display array via pointer */
display_array(pa,MAX);


/* display array via array name */

display_array(a,MAX);


return
0;
}


/*

Display an array of integers with predefined size
*/

void display_array(int*
p,const
intsize)
{

int
i;
for(i
= 0;
i <
size;
i++)

{
printf("%d ",p[i]);

}
printf("\n");

}

In this tutorial, you have learned about C pointer, which is a special variable that store memory address of another variable. You also learned the relationship between a pointer and an array, and how to manipulate an array through a pointer.

转载自:http://www.zentut.com/c-tutorial/c-pointer/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: