Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

fonksiyon cözemedim

Başlatan armsistem, 08 Eylül 2011, 16:58:14

armsistem

Arkadaşlar selam ; aşağıdaki fonksiyonun mantığını anlayamadım ... ne anlama geliyor.


void brsPrintf (const char* str, ...);

RcALTIN

#1
Alıntı yapılan: armsistem - 08 Eylül 2011, 16:58:14
Arkadaşlar selam ; aşağıdaki fonksiyonun mantığını anlayamadım ... ne anlama geliyor.


void brsPrintf (const char* str, ...);

yıldızdan dolayı str değişkeni metoda gönderilen değişkeni referans alıyor, üç nokta -emin olmamakla birlikte- istenildiği kadar parametre yüklenebileceğini belirtiyor.

yani brsPrintf(a,b,c,d);
diye yollarsak metod içinde str değişkenine yaptığımız müdehaleler direkt a üzerine uygulanır ama değişken neden const(değiştirilmesini engelleyen bir keyword) olarak tanımlı bunu bende anlamadım, diğer değişkenler büyük ihtimalle bir dizi olarak alınıyordur ama yöntemini ben bilmiyorum, daha önce hiç kullanmadığım bir syntax üç nokta...
KİMSEYİ ENGELLEDİĞİM YOK, ÖZEL İLETİ DEVRE DIŞI !

LukeSkywalker

Hocam "..." diye birşey yok. Kodu nereden aldıysanız bir link verir misiniz?
const char *str =const char str[];

Bunun const olarak kullanılmasının sebebi şu:
brsPrintf("picproje") yazdığımızı varsayalım. picproje stringi bir sabittir. Fonksiyon içerisine alınabilmesi için const dizi tanımlanmalıdır.


JKramer

Bu da CCS yardım dosyasından daha basit bir örnek, anlaması daha kolay:
Alıntı YapThe compiler supports a variable number of parameters. This works like the ANSI requirements except that it does not require at least one fixed parameter as ANSI does. The function can be passed any number of variables and any data types. The access functions are VA_START, VA_ARG, and VA_END. To view the number of arguments passed, the NARGS function can be used.
/*

stdarg.h holds the macros and va_list data type needed for variable number of parameters.

*/

#include <stdarg.h>

 
A function with variable number of parameters requires two things. First, it requires the ellipsis (...), which must be the last parameter of the function. The ellipsis represents the variable argument list. Second, it requires one more variable before the ellipsis (...). Usually you will use this variable as a method for determining how many variables have been pushed onto the ellipsis.

 
Here is a function that calculates and returns the sum of all variables:

int Sum(int count, ...)

{

   //a pointer to the argument list

   va_list al;

   int x, sum=0;

   //start the argument list

   //count is the first variable before the ellipsis

   va_start(al, count);

   while(count--) {

      //get an int from the list

      x = var_arg(al, int);

      sum += x;

   }

   //stop using the list

   va_end(al);

   return(sum);

}

 
Some examples of using this new function:

x=Sum(5, 10, 20, 30, 40, 50);

y=Sum(3, a, b, c);