<< Chapter < Page Chapter >> Page >

free(pa);

free(pb);

Một số phép toán trên con trỏ

a. Phép gán con trỏ: Hai con trỏ cùng kiểu có thể gán cho nhau.

Ví dụ:

int a, *p, *a ; float *f;

a = 5 ; p =&a ; q = p ; /* đúng */

f = p ; /* sai do khác kiểu */

Ta cũng có thể ép kiểu con trỏ theo cú pháp:

(<Kiểu kết quả>*)<Tên con trỏ>

Chẳng hạn, ví dụ trên được viết lại:

int a, *p, *a ; float *f;

a = 5 ; p =&a ; q = p ; /* đúng */

f = (float*)p; /* Đúng nhờ ép kiểu*/

b. Cộng, trừ con trỏ với một số nguyên

Ta có thể cộng (+), trừ (-) 1 con trỏ với 1 số nguyên N nào đó; kết quả trả về là 1 con trỏ. Con trỏ này chỉ đến vùng nhớ cách vùng nhớ của con trỏ hiện tại N phần tử.

Ví dụ: Cho đoạn chương trình sau:

int *pa;

pa = (int*) malloc(20); /* Cấp phát vùng nhớ 20 byte=10 số nguyên*/

int *pb, *pc;

pb = pa + 7;

pc = pb - 3;

Lúc này hình ảnh của pa, pb, pc như sau:

0 1 2 3 4 5 6 7 8 9
pa pc pb

c. Con trỏ NULL: là con trỏ không chứa địa chỉ nào cả. Ta có thể gán giá trị NULL cho 1 con trỏ có kiểu bất kỳ.

d. Lưu ý:

- Ta không thể cộng 2 con trỏ với nhau.

- Phép trừ 2 con trỏ cùng kiểu sẽ trả về 1 giá trị nguyên (int). Đây chính là khoảng cách (số phần tử) giữa 2 con trỏ đó. Chẳng hạn, trong ví dụ trên pc-pa=4.

Con trỏ và mảng

Con trỏ và mảng 1 chiều

Giữa mảng và con trỏ có một sự liên hệ rất chặt chẽ. Những phần tử của mảng có thể được xác định bằng chỉ số trong mảng, bên cạnh đó chúng cũng có thể được xác lập qua biến con trỏ.

Truy cập các phần tử mảng theo dạng con trỏ

Ta có các quy tắc sau:

&<Tên mảng>[0] tương đương với<Tên mảng>

&<Tên mảng>[<Vị trí>] tương đương với<Tên mảng>+<Vị trí>

<Tên mảng>[<Vị trí>] tương đương với *(<Tên mảng>+<Vị trí>)

Ví dụ: Cho 1 mảng 1 chiều các số nguyên a có 5 phần tử, truy cập các phần tử theo kiểu mảng và theo kiểu con trỏ.

#include<stdio.h>

#include<conio.h>

/* Nhập mảng bình thường*/

void NhapMang(int a[], int N){

int i;

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

{

printf("Phan tu thu %d: ",i);scanf("%d",&a[i]);

}

}

/* Nhập mảng theo dạng con trỏ*/

void NhapContro(int a[], int N)

{

int i;

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

printf("Phan tu thu %d: ",i);scanf("%d",a+i);

}

}

int main()

{

int a[20],N,i;

clrscr();

printf("So phan tu N= ");scanf("%d",&N);

NhapMang(a,N); /* NhapContro(a,N)*/

printf("Truy cap theo kieu mang: ");

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

printf("%d ",a[i]);

printf("\nTruy cap theo kieu con tro: ");

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

printf("%d ",*(a+i));

getch();

return 0;

}

Kết quả thực thi của chương trình:

Truy xuất từng phần tử đang được quản lý bởi con trỏ theo dạng mảng

<Tên biến>[<Vị trí>] tương đương với *(<Tên biến>+<Vị trí>)

&<Tên biến>[<Vị trí>] tương đương với (<Tên biến>+<Vị trí>)

Trong đó<Tên biến>là biến con trỏ,<Vị trí>là 1 biểu thức số nguyên.

Ví dụ: Giả sử có khai báo:

#include<stdio.h>

#include<alloc.h>

#include<conio.h>

int main(){

int *a;

int i;

clrscr();

a=(int*)malloc(sizeof(int)*10);

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

a[i] = 2*i;

printf("Truy cap theo kieu mang: ");

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

printf("%d ",a[i]);

printf("\nTruy cap theo kieu con tro: ");

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

printf("%d ",*(a+i));

getch();

return 0;

}

Kết quả chương trình:

Với khai báo ở trên, hình ảnh của con trỏ a trong bộ nhớ:

0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
a 2 byte

Con trỏ chỉ đến phần tử mảng

Giả sử con trỏ ptr chỉ đến phần tử a[i] nào đó của mảng a thì:

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Am nhac. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10735/1.3
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Am nhac' conversation and receive update notifications?

Ask