Senin, 02 November 2009

tugas pointer c ++

Kita dapat mendefinisikan sebuah variabel di C + + untuk menyimpan alamat memori. A pointer in C++ is said to "point to" the memory address that is stored in it. Suatu pointer di C + + adalah kata untuk "menunjukkan" alamat memori yang disimpan di dalamnya. Also, when defining a C++ pointer variable, we must specify the type of variable to which it is pointing. Juga, ketika menentukan sebuah C + + variabel pointer, kita harus menentukan jenis variabel yang menunjuk. For example, to define a pointer, which will store a memory address at which exists an int, we can do the following: Misalnya, untuk mendefinisikan sebuah pointer, yang akan menyimpan alamat memori di mana ada yang int, kita dapat melakukan hal berikut:

//Sample program for c++ pointer / / Contoh program untuk c + + pointer
signed main() ditandatangani main ()
{ (
int* p; int * p;
//Right now, p contains no particular myval in this C++ code. / / Sekarang, p tidak berisi myval tertentu dalam hal ini C + + code.
} )

The asterisk in the above specifies that we have a pointer variable. Let's say we want to define an int variable and then we want to define a pointer variable, which will store the memory address of this int: Tanda bintang di atas menetapkan bahwa kita memiliki variabel pointer. Andaikan saja kita ingin mendefinisikan variabel int dan kemudian kita ingin mendefinisikan sebuah variabel pointer, yang akan menyimpan alamat memori int ini:

//c++ pointer using an int variable / / c + + pointer menggunakan variabel int

signed main() ditandatangani main ()
{ (
int myval(7); int myval (7);
int* p_myval; int * p_myval;
//Right now, p_myval contains no particular myval. / / Sekarang, p_myval tidak mengandung myval tertentu.
p_myval = &myval; p_myval = &myval;
//Now, p_myval in this c++ program contains the memory address of the variable myval / / Sekarang, p_myval di c + + program berisi alamat memori dari variabel myval
} )

With &myval, & is referred to as "the address-of operator". Dengan & myval, & disebut sebagai "alamat-of operator". The expression &myval is of the c++ type int*. Ekspresi & myval adalah dari c + + tipe int *. We then store this int* myval in our int* variable, which is p_myval. Now, we will actually use this pointer: Kami

kemudian menyimpan int * myval di int * variabel, yang p_myval. Sekarang, kita akan benar-benar menggunakan pointer ini:

//Sample program for c++ pointer / / Contoh program untuk c + + pointer

signed main() ditandatangani main ()
{ (
int myval = 7; int myval = 7;
int* p_myval = &myval; int * p_myval = &myval;
*p_myval = 6; * p_myval = 6;
} )

With *p_myval = 6, the asterisk is referred to as "the dereference operator". Dengan * p_myval = 6, bintang disebut sebagai "dereference operator". It turns the expression from an int* into an int. Ternyata ekspresi dari sebuah int * menjadi int. The statement has the effect of setting the myval of myval to 6. So now what are the uses of pointers in c++ ? Pernyataan memiliki efek pengaturan myval dari myval sampai 6. Jadi sekarang apa penggunaan pointer di c + +? Let us see something about how and when they should be used: Mari kita melihat sesuatu tentang bagaimana dan kapan mereka harus digunakan:

A) When the pointer must be re-seated. A) Ketika pointer harus kembali duduk.

B) When arrays are involved. B) Ketika array yang terlibat.

Consider a string, an array of characters: Pertimbangkan sebuah string, array karakter:

signed int main() ditandatangani int main ()
{ (
char my_name[] = "Code"; char my_name [] = "Kode";
} )

Here's what the string (char c++ pointer) looks like in memory: Inilah yang string (char c + + pointer) tampak seperti di memori:

Name of Variable Nama Variabel Type of Variable Jenis Variabel Address in Memory Alamat di memori Value Stored Nilai Stored
my_name my_name Char Char 108 108 'C' 'C'

char char 109 109 'o' 'o'

char char 110 110 'd' 'd'

char char 111 111 'e' 'e'

char char 112 112 '\0' '\ 0'
While accessing the characters inside the variable my_name, the position of the first character will start from 0. Sementara mengakses karakter dalam variabel my_name, posisi karakter pertama akan mulai dari 0. So the array of size 4 will be accessed for characters from 0, 1, 2 and 3. We can define a pointer to point to the second element in the array my_name, as so: Jadi ukuran array 4 akan diakses untuk karakter dari 0, 1, 2 dan 3. Kita dapat mendefinisikan sebuah pointer untuk menunjuk ke kedua elemen dalam array my_name, seperti jadi:

int main() int main ()
{ (
char my_name[] = "Code"; char my_name [] = "Kode";
char* k( &my_name[1] ); char * k (& my_name [1]);
} )

Now, what k points to looks like so in memory: Sekarang, apa yang menunjuk k tampak seperti di memori:

Name of Variable Nama Variabel Type of Variable Jenis Variabel Address in Memory Alamat di memori Value Stored Nilai Stored
my_name my_name Char* Char * 116 116 109 109

char char 109 109 'o' 'o'

char char 110 110 'd' 'd'

char char 111 111 'e' 'e'

char char 112 112 '\0' '\ 0'
So that's one usage of c++ pointers there, to point to an individual object in an array. The other feature of c++ pointers is that they can be "re-seated", which means that you can change their value, you can change what they're pointing to, as in the following: // c++ pointer program for modifying values/re-seating. Jadi itu salah satu penggunaan dari c + + pointer di sana, untuk menunjuk ke objek individu dalam array. Fitur lain dari c + + pointer adalah bahwa mereka dapat "kembali duduk", yang berarti Anda dapat mengubah nilai mereka, Anda dapat mengubah apa yang mereka 'kembali menunjuk, sebagai berikut: / / c + + program untuk memodifikasi pointer nilai / re-tempat duduk.

signed int main() ditandatangani int main ()
{ (
int myval(5); int myval (5);
int myvalue2 = 7; int myvalue2 = 7;
int* p_primate; int * p_primate;
p_primate = &myval; p_primate = &myval;
*p_primate = 9; * p_primate = 9;
p_primate = &myvalue2; p_primate = &myvalue2;
*p_primate = 10; * p_primate = 10;
} )

Guess what kind of variable we have in the following: Coba tebak jenis variabel yang kita miliki dalam berikut:

signed main() ditandatangani main ()
{ (
signed** p_p_cow; ditandatangani ** p_p_cow;
} )

An int* c++ pointer points to an int, so an int** points to an int*. Sebuah int * c + + pointer menunjuk ke suatu int, jadi sebuah int ** menunjuk ke suatu int *. In English: The variable p_cow above stores a memory address. Dalam bahasa Inggris: p_cow Variabel di atas menyimpan sebuah alamat memori. At that memory address exists a variable of type int*. Pada saat itu ada alamat memori variabel bertipe int *. This int* variable also stores a memory address, at which exists an int. Int * Variabel ini juga menyimpan alamat memori, di mana ada sebuah int. Take the following: Ambil berikut:

//Snippet for c++ pointer to pointers / / Snippet untuk c + + pointer ke pointer

int main() int main ()
{ (
int cow(7); int sapi (7);
int* p_cow = &cow; int * p_cow = &cow;
int** p_p_cow(&p_cow); int ** p_p_cow (& p_cow);
int*** p_p_p_cow = &p_p_cow; int *** p_p_p_cow = & p_p_cow;
} )

Here's what the above c++ pointers look like in memory: Inilah yang di atas c + + pointer terlihat seperti di memori:

Name of Variable Nama Variabel Type of Variable Jenis Variabel Address in Memory Alamat di memori Value Stored Nilai Stored
Cow Sapi Int Int 108 108 7 7
p_cow p_cow int* int * 110 110 108 108
p_p_cow p_p_cow int** int ** 112 112 110 110
p_p_p_cow p_p_p_cow int*** int *** 114 114 112 112
With the above code, we can set the value of cow using p_p_p_cow: Dengan kode diatas, kita dapat mengatur nilai sapi menggunakan p_p_p_cow:

//Using c++ pointer to pointer / / Menggunakan c + + pointer ke pointer

int main() int main ()
{ (
int cow(7); int sapi (7);
int* p_cow = &cow; int * p_cow = &cow;
int** p_p_cow(&p_cow); int ** p_p_cow (& p_cow);
int*** p_p_p_cow = &p_p_cow; int *** p_p_p_cow = & p_p_cow;
***p_p_p_cow = 8; *** p_p_p_cow = 8;
} )

C++ Pointers are commonly used when working with strings. C + + Pointer biasanya digunakan ketika bekerja dengan string. Let's define a function; this function will be supplied with a string. Mari kita mendefinisikan sebuah fungsi fungsi ini akan ditawarkan dengan sebuah string. We're going to change the 2nd, 5th and 7th characters of the string: Kami akan mengubah ke-2, 5 dan ke-7 karakter dari string:

void ChangeString(char* const p_first_char) void ChangeString (const char * p_first_char)
{ (
p_first_char[1] = 'a'; p_first_char [1] = 'a';
p_first_char[4] = 'b'; p_first_char [4] = 'b';
p_first_char[6] = 'c'; p_first_char [6] = 'c';
} )

Or we can define a function, which will be supplied with a string. Atau kita dapat mendefinisikan sebuah fungsi, yang akan disertakan dengan sebuah string. The function will return the first instance of the character 't' in the string: Fungsi akan mengembalikan contoh pertama dari karakter 't' dalam string:

char* GetFirstT(char* p_first_char) char * GetFirstT (char * p_first_char)
{ (

for ( ; *p ; ++p) for (; * p; + + p)
{ (
if ( *p == 't' ) return p; if (* p == 't') return p;
} )
return 0; return 0;

} )

signed main() ditandatangani main ()
{ (

char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; char the_alphabet [] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet); char * p_t = GetFirstT (the_alphabet);

} )


Now I'm going to talk about c++ pointers and constness . Sekarang aku akan bicara tentang c + + pointer dan constness. If you want a const c++ pointer variable, a c++ pointer variable whose value you can't change after initialization, then stick the const directly beside the variable's name: Jika Anda menginginkan const c + + pointer variabel, yang c + + pointer variabel yang nilainya tidak dapat berubah setelah inisialisasi, kemudian menancapkan const langsung di samping nama variabel:

signed main() ditandatangani main ()
{ (

int myval = 5; int myval = 5;
int myvalue2(8); int myvalue2 (8);
int* const p_k = &myval; int * const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR p_k = &myvalue2; / / compile ERROR
*p_k = 3; //No problem, the variable to which it points is non-const * p_k = 3; / / Tidak masalah, variabel yang menunjuk adalah non-const

} )

If you want a non-const c++ pointer variable, but you want the variable to which it points to be const, then: Jika Anda ingin non-const pointer c + + variabel, tetapi Anda ingin variabel yang poin yang harus const, maka:

signed main() ditandatangani main ()
{ (

int myval(7); int myval (7);
int myvalue2 = 6; int myvalue2 = 6;
const int* p_k = &myval; const int * p_k = &myval;
p_k = &myvalue2; //No problem, the variable is non-const p_k = &myvalue2; / / Tidak masalah, variabel adalah non-const
*p_k = 7; //COMPILE ERROR * p_k = 7; / / compile ERROR

} )

If you want a const c++ pointer that points to a const variable then: Jika Anda menginginkan const c + + pointer yang menunjuk ke suatu variabel const maka:

signed int main() ditandatangani int main ()
{ (

int myval(17); int myval (17);
int myvalue2 = 4; int myvalue2 = 4;
const int* const p_k = &myval; const int * const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR p_k = &myvalue2; / / compile ERROR
*p_k = 32; //COMPILE ERROR * p_k = 32; / / compile ERROR

} )


You Can Rate this Article, if you are Logged In Anda Dapat Rate Pasal ini, jika Anda Logged In

More Links from CoderSource.net: More Link dari CoderSource.net:


Refer to a Friend: Rujuk ke Teman:

Your Details: Your Details:

Name: Nama: e-mail: e-mail:

Friend Details: Teman Rincian:

Name: Nama: e-mail: e-mail:


Tidak ada komentar:

Posting Komentar