<< Chapter < Page Chapter >> Page >

ptr + j chỉ đến phần tử thứ j sau a[i], tức a[i+j]

ptr - j chỉ đến phần tử đứng trước a[i], tức a[i-j]

Ví dụ: Giả sử có 1 mảng mang_int, cho con trỏ contro_int chỉ đến phần tử thứ 5 trong mảng. In ra các phần tử của contro_int&mang_int.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

int main()

{

int i,mang_int[10];

int *contro_int;

clrscr();

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

mang_int[i]=i*2;

contro_int=&mang_int[5];

printf("\nNoi dung cua mang_int ban dau=");

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

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

printf("\nNoi dung cua contro_int ban dau =");

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

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

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

contro_int[i]++;

printf("\n--------------------------------------------------------");

printf("\nNoi dung cua mang_int sau khi tang 1=");

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

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

printf("\nNoi dung cua contro_int sau khi tang 1=");

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

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

if (contro_int!=NULL)

free(contro_int);

getch();

return 0;

}

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

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

Ta có thể sử dụng con trỏ thay cho mảng nhiều chiều như sau:

Giả sử ta có mảng 2 chiều và biến con trỏ như sau:

int a[n][m];

int *contro_int;

Thực hiện phép gán contro_int=a;

Khi đó phần tử a[0][0]được quản lý bởi contro_int;

a[0][1]được quản lý bởi contro_int+1;

a[0][2]được quản lý bởi contro_int+2;

...

a[1][0]được quản lý bởi contro_int+m;

a[1][1]được quản lý bởi contro_int+m+1;

...

a[n][m]được quản lý bởi contro_int+n*m;

Tương tự như thế đối với mảng nhiều hơn 2 chiều.

Ví dụ: Sự tương đương giữa mảng 2 chiều và con trỏ.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

int main()

{

int i,j;

int mang_int[4][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,

15,16,17,18,19,20};

int *contro_int;

clrscr();

contro_int=(int*)mang_int;

printf("\nNoi dung cua mang_int ban dau=");

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

{

printf("\n");

for (j=0;j<5;j++)

printf("%d\t",mang_int[i][j]);

}

printf("\n---------------------------------");

printf("\nNoi dung cua contro_int ban dau \n");

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

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

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

contro_int[i]++;

printf("\n--------------------------------------------------------");

printf("\nNoi dung cua mang_int sau khi tang 1=");

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

{

printf("\n");

for (j=0;j<5;j++)

printf("%d\t",mang_int[i][j]);

}

printf("\nNoi dung cua contro_int sau khi tang 1=\n");

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

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

if (contro_int!=NULL)

free(contro_int);

getch();

return 0;

}

Kết quả thực hiện chương trình như sau:

***SORRY, THIS MEDIA TYPE IS NOT SUPPORTED.***

Con trỏ và tham số hình thức của hàm

Khi tham số hình thức của hàm là một con trỏ thì theo nguyên tắc gọi hàm ta dùng tham số thực tế là 1 con trỏ có kiểu giống với kiểu của tham số hình thức. Nếu lúc thực thi hàm ta có sự thay đổi trên nội dung vùng nhớ được chỉ bởi con trỏ tham số hình thức thì lúc đó nội dung vùng nhớ được chỉ bởi tham số thực tế cũng sẽ bị thay đổi theo.

Ví dụ : Xét hàm hoán vị được viết như sau :

#include<stdio.h>

#include<conio.h>

void HoanVi(int *a, int *b)

{

int c=*a;

*a=*b;

*b=c;

}

int main()

{

int m=20,n=30;

clrscr();

printf("Truoc khi goi ham m= %d, n= %d\n",m,n);

HoanVi(&m,&n);

printf("Sau khi goi ham m= %d, n= %d",m,n);

getch();

return 0;

}

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

M=20n=30&M&Nm=20n=30abm=30n=20&M&Nm=30n=20&M&Ntrước khi gọi hàmkhi gọi hàmsau khi gọi hàm:

A=&M; b=&N; con trỏ a, b bị giải phóng

Lúc này : *a=m; *b=n; m, n đã thay đổi:

Đổi chỗ ta được :

*a=m=30; *b=n=20;

Bài tập

Mục tiêu

Tiếp cận với một kiểu dữ liệu rất mạnh trong C là kiểu con trỏ. Từ đó, sinh viên có thể xây dựng các ứng dụng bằng cách sử dụng cấp phát động thông qua biến con trỏ.

Nội dung

Thực hiện các bài tập ở chương trước (chương VI : Kiểu mảng) bằng cách sử dụng con trỏ.

Questions & Answers

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
cell is the building block of life.
Condoleezza Reply
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