1) delkaracja wskaźnika funkcji,
2) przypisanie wartosci do tego wskaźnika,
3) no i wykonanie takiej (wskazywanej) funkcji
plz help

 
Aktyw Forum
Zarejestruj się na forum.ep.com.pl i zgłoś swój akces do Aktywu Forum. Jeśli jesteś już zarejestrowany wystarczy, że się zalogujesz.
Sprawdź punkty Zarejestruj sięModeratorzy:Jacek Bogusz, Moderatorzy

czy ktos mi moze powiedziec jak bedzie wygladac w języku C:
1) delkaracja wskaźnika funkcji,
2) przypisanie wartosci do tego wskaźnika,
3) no i wykonanie takiej (wskazywanej) funkcji
plz help
Kod: Zaznacz cały
1)    int (*p_fun) ();   // wskaznik na funkcje bezargumentowa, zwracajaca int
 
2)    p_fun = nazwa_funkcji;    // jak wiadomo, nazwa funkcji jest tez adresem jej poczatku, nazwa tablicy jest adresem jej pierwszego elementu, i wogole w C, nazwa jest poczatkiem danej struktury
3) if (p_fun)      // if tak sobie, zeby sprawdzic czy pointer wogole na cos wskazuje
       (*p_fun) ();
 Info o wskazniku znajdziesz
 Info o wskazniku znajdziesz   tu
 tuKod: Zaznacz cały
/* Demonstrates basic pointer use. */
#include <stdio.h>
/* Declare and initialize an int variable */
int var = 1;
/* Declare a pointer to int */
int *ptr;
main()
{
      /* Initialize ptr to point to var */
      ptr = &var;
      /* Access var directly and indirectly */
      printf("\nDirect access, var = %d", var);
      printf("\nIndirect access, var = %d", *ptr);
      /* Display the address of var two ways */
      printf("\n\nThe address of var = %d", &var);
      printf("\nThe address of var = %d\n", ptr);
     
      puts
      ("\nPress Enter to end program ");
      getchar(); // ACeK
      return 0;
}Kod: Zaznacz cały
/* Demonstrates the relationship between addresses and */
/* elements of arrays of different data types. */
#include <stdio.h>
/* Declare three arrays and a counter variable. */
int i[10], x;
float f[10];
double d[10];
main()
{
      /* Print the table heading */
      printf("\t\tInteger\t\tFloat\t\tDouble");
      printf("\n================================");
      printf("=========================");
      /* Print the addresses of each array element. */
      for (x = 0; x < 10; x++)
          printf("\nElement %d:\t%lo\t%ld\t\t%ld\t\t%lx", x, &i[x], &f[x], &d[x], &i[x]);
      printf("\n================================");
      printf("=========================\n");
     
      puts
      ("\nPress Enter to end program ");
      getchar(); // ACeK
return 0;
}
Kod: Zaznacz cały
/* Demonstrates using pointer arithmetic to access */
/* array elements with pointer notation. */
#include <stdio.h>
#define MAX 10
/* Declare and initialize an integer array. */
int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 };
/* Declare a pointer to int and an int variable. */
int *i_ptr, count;
/* Declare and initialize a float array. */
float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 };
/* Declare a pointer to float. */
float *f_ptr;
main()
{
      /* Initialize the pointers. */
      i_ptr = i_array;
      f_ptr = f_array;
      /* Print the array elements. */
      for (count = 0; count < MAX; count++)
          printf("%d\t%f\n", *i_ptr++, *f_ptr++);
     
      puts
      ("\nPress Enter to end program ");
      getchar(); // ACeK
      return 0;
}
 
 
AktualnieWykonywanaFunkcja, jak widac jest typu int32, a Ty probujesz jej przypisac pointer. To nic ze defacto (przy architekturze jakiej domyslam sie ze uzywasz) jest to zapisane w takiej samej liczbie, ale kompilator sie przyburzy. Mozesz sprobowac to rzutowac, wowczas sie nie przyburzy, ale:Dzięki wielkie ...
[ Dodano: 17-05-2005, 14:50 ]
co mam zrobić żeby było elegncko (kompilator sie burzy : "illegal conversion pointer to integer")
int32 AktualnieWykonywanaFunkcja=0;
void JakasFunkcja (void)
{
//... blebleble...
AktualnieWykonywanaFunkcja = JakasFunkcja
}
z góry dzięki

 
 Kod: Zaznacz cały
/* Demonstration of declaring and using a pointer to a function.*/
#include <stdio.h>
/* The function prototype. */
double square(double x);
/* The pointer declaration. */
double (*p)(double x);
main()
{
      /* Initialize p to point to square(). */
      p = square;
      /* Call square() two ways. */
      printf("%f %f\n", square(6.6), p(6.6));
 
      puts
      ("\nPress Enter to end program ");
      getchar(); // ACeK
 
      return(0);
}
double square(double x)
{
      return x * x;
}
Kod: Zaznacz cały
/* Using a pointer to call different functions. */
#include <stdio.h>
/* The function prototypes. */
void func1(int x);
void one(void);
void two(void);
void other(void);
main()
{
      int a;
      for (;;)
      {
          puts("\nEnter an integer between 1 and 10, 0 to exit: ");
          scanf("%d", &a);
          if (a == 0)
             break;
          func1(a);
      }
      return(0);
}
void func1(int x)
{
      /* The pointer to function. */
      void (*ptr)(void);
      if (x == 1)
          ptr = one;
      else if (x == 2)
          ptr = two;
      else
          ptr = other;
      ptr();
}
void one(void)
{
      puts("You entered 1.");
}
void two(void)
{
      puts("You entered 2.");
}
void other(void)
{
      puts("You entered something other than 1 or 2.");
}
 
 
Dzięki wielkie ...
[ Dodano: 17-05-2005, 14:50 ]
co mam zrobić żeby było elegncko (kompilator sie burzy : "illegal conversion pointer to integer")
int32 AktualnieWykonywanaFunkcja=0;
void JakasFunkcja (void)
{
//... blebleble...
AktualnieWykonywanaFunkcja = JakasFunkcja
}
z góry dzięki
Użytkownicy przeglądający to forum: Obecnie na forum nie ma żadnego zarejestrowanego użytkownika i 0 gości