C İşaretçileri (Örneklerle)

Bu eğitimde, işaretçiler hakkında bilgi edineceksiniz; işaretçiler nelerdir, bunları nasıl kullanıyorsunuz ve örnekler yardımıyla onlarla çalışırken karşılaşabileceğiniz yaygın hatalar.

İşaretçiler, C ve C ++ programlamanın güçlü özellikleridir. İşaretçileri öğrenmeden önce, C programlamadaki adresleri öğrenelim.

C adres

Programınızda değişken bir var varsa, size &varbellekteki adresini verecektir.

scanf()İşlevi kullanırken adresi defalarca kullandık .

 scanf("%d", &var);

Burada kullanıcı tarafından girilen değer, var değişkeninin adresinde saklanır. Çalışan bir örnek alalım.

 #include int main() ( int var = 5; printf("var: %d", var); // Notice the use of & before var printf("address of var: %p", &var); return 0; ) 

Çıktı

 var: 5 var adresi: 2686778

Not: Yukarıdaki kodu çalıştırdığınızda muhtemelen farklı bir adres alacaksınız.

C İşaretçiler

İşaretçiler (işaretçi değişkenleri), değerleri değil adresleri saklamak için kullanılan özel değişkenlerdir.

İşaretçi Sözdizimi

İşte işaretçileri nasıl ilan edebileceğimiz.

 int* p;

Burada, p inttipi bir işaretçi ilan ettik .

Ayrıca bu yollarla işaretçi bildirebilirsiniz.

 int *p1; int * p2; 

İşaretçi bildirmenin başka bir örneğini ele alalım.

 int* p1, p2;

Burada, bir işaretçi p1 ve bir normal değişken p2 tanımladık.

İşaretçilere adres atama

Bir örnek alalım.

 int* pc, c; c = 5; pc = &c; 

Burada c değişkenine 5 atanmıştır. Ve c'nin adresi pc işaretçisine atanır.

İşaretçilerin Gösterdiği Şeylerin Değerini Alın

İşaretçilerle gösterilen şeyin değerini elde etmek için *operatörü kullanırız . Örneğin:

 int* pc, c; c = 5; pc = &c; printf("%d", *pc); // Output: 5

Burada adresi cpc işaretçisine atanır. Bu adreste kayıtlı değeri almak için * pc kullandık.

Not: Yukarıdaki örnekte, pc bir göstericidir, değil *pc. Böyle bir şey yapamazsınız ve yapmamalısınız *pc = &c;

Bu arada, *dereference operatörü (işaretçilerle çalışırken) denir. Bir işaretçi üzerinde çalışır ve o işaretçide saklanan değeri verir.

İşaretçilerin İşaret Ettiği Değeri Değiştirme

Bir örnek alalım.

 int* pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Ouptut: 1

C'nin adresini pc işaretçisine atadık.

Sonra c'nin değerini 1 olarak değiştirdik. Pc ve c'nin adresi aynı olduğu için *pcbize 1 verir.

Başka bir örnek alalım.

 int* pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Ouptut: 1 printf("%d", c); // Output: 1 

C'nin adresini pc işaretçisine atadık.

Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is the same, c will be equal to 1.

Let's take one more example.

 int* pc, c, d; c = 5; d = -15; pc = &c; printf("%d", *pc); // Output: 5 pc = &d; printf("%d", *pc); // Ouptut: -15

Initially, the address of c is assigned to the pc pointer using pc = &c;. Since c is 5, *pc gives us 5.

Then, the address of d is assigned to the pc pointer using pc = &d;. Since d is -15, *pc gives us -15.

Example: Working of Pointers

Let's take a working example.

 #include int main() ( int* pc, c; c = 22; printf("Address of c: %p", &c); printf("Value of c: %d", c); // 22 pc = &c; printf("Address of pointer pc: %p", pc); printf("Content of pointer pc: %d", *pc); // 22 c = 11; printf("Address of pointer pc: %p", pc); printf("Content of pointer pc: %d", *pc); // 11 *pc = 2; printf("Address of c: %p", &c); printf("Value of c: %d", c); // 2 return 0; ) 

Output

 Address of c: 2686784 Value of c: 22 Address of pointer pc: 2686784 Content of pointer pc: 22 Address of pointer pc: 2686784 Content of pointer pc: 11 Address of c: 2686784 Value of c: 2 

Explanation of the program

  1. int* pc, c;

    Here, a pointer pc and a normal variable c, both of type int, is created.
    Since pc and c are not initialized at initially, pointer pc points to either no address or a random address. And, variable c has an address but contains random garbage value.
  2. c = 22;

    This assigns 22 to the variable c. That is, 22 is stored in the memory location of variable c.
  3. pc = &c;

    This assigns the address of variable c to the pointer pc.
  4. c = 11;

    This assigns 11 to variable c.
  5. *pc = 2;

    This change the value at the memory location pointed by the pointer pc to 2.

Common mistakes when working with pointers

Suppose, you want pointer pc to point to the address of c. Then,

 int c, *pc; // pc is address but c is not pc = c; // Error // &c is address but *pc is not *pc = &c; // Error // both &c and pc are addresses pc = &c; // both c and *pc values *pc = c;

Here's an example of pointer syntax beginners often find confusing.

 #include int main() ( int c = 5; int *p = &c; printf("%d", *p); // 5 return 0; )

Why didn't we get an error when using int *p = &c;?

It's because

 int *p = &c;

is equivalent to

 int *p: p = &c;

Her iki durumda da, bir işaretçi oluşturuyoruz p(değil *p) ve &cona atıyoruz .

Bu karışıklığı önlemek için şu ifadeyi kullanabiliriz:

 int* p = &c;

Artık işaretçilerin ne olduğunu biliyorsunuz, sonraki derste işaretçilerin dizilerle nasıl ilişkili olduğunu öğreneceksiniz.

Ilginç makaleler...