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:
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
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:
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:
Here's what the string (char c++ pointer) looks like in memory: Inilah yang string (char c + + pointer) tampak seperti di memori: 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:
Now, what k points to looks like so in memory: Sekarang, apa yang menunjuk k tampak seperti di memori: 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.
Guess what kind of variable we have in the following: Coba tebak jenis variabel yang kita miliki dalam berikut:
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
Here's what the above c++ pointers look like in memory: Inilah yang di atas c + + pointer terlihat seperti di memori: 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
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:
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:
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:
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:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
More Links from CoderSource.net: More Link dari CoderSource.net:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Senin, 02 November 2009
tugas pointer c ++
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar