指针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.
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;
You specified variable name (
unary operator
printf("The memory address of xis %p\n",&x);
In our system, the memory address of xis:
The memory address of xis 0028FF1C
Because memory address of
In C, we say that
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.
<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
Second, you place the indirection operator (
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
The following example defines 2 pointers that pointto
int*px, *py;
char*ps;
To initialize a pointer, you assign the memory address of another variable to the pointer using the address-of operator (
pointer = &variable;
For example, to assign the address of the
px= &x;
For example, we can display the value of xthrough the
printf("%d",*px);
We can change the value of
*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;
}
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
Let’s take a look at an example.
inta[10];
This declares an array of 10 integers. Underneath, there is a block of memory with 10 consecutive objects defining as follows:
The
int* pa;
pa = &a[0];
Because the
Recall that the name of an array is the memory address of the first element, therefore the following assignment:
pa = &a[0];
can be rewritten as follows:
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");
}
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/
Introduction to C Pointer
When you define avariable 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; |
x), its data type
intand its value,
10. The variable
xresides 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); |
The memory address of xis 0028FF1C
1 | The memory address of x is 0028FF1C |
xis a number (
28FF1C), you can use another variable to store it e.g.,
pxas the following picture:
In C, we say that
pxpoints to x, or
pxis 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>; |
C data type such as
int,
char,
floator 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
pto help distinguish between a pointer and a variable in your programs.
The following example defines 2 pointers that pointto
intvariables, an a pointer that points to a
charvariable.
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; |
xvariable 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
pxpointer as follows:
printf("%d",*px);
1 | printf("%d",*px); |
xthrough the
pxpointer 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 */ |
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; } |
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]; |
The
a[i]refers the ith element of the
aarray. The following defines a pointer that points to the first element of the
aarray:
int* pa;
pa = &a[0];
1 2 | int * pa; pa = &a[0]; |
papointer points to the first element of the
aarray,
*pais 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]; |
pa = a;
1 | pa = a; |
/*
* 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"); } |
转载自:http://www.zentut.com/c-tutorial/c-pointer/
相关文章推荐