“C struct pointer berfungsi” Kode Jawaban

C struct pointer berfungsi


//structs pointing to functions
#include <stdio.h>
// struct
typedef struct audio
{
    // All of these are asignable pointers to functions
    void (*play_ptr)();
    void (*stop_ptr)();
    void (*pause_ptr)();
    void (*forward_ptr)();
    void (*back_ptr)();
} audio;

// This function starts a new line in the console out put;
void space()
{
    printf("\n");
}
// five different funtions for the pointers in the struct to be pointed at.
void play_function()
{
    printf("Play >");
    space();
}

void stop_function()
{
    printf("Stop []");
    space();
}

void pause_function()
{
    printf("Pause ||");
    space();
}

void forward_function()
{
    printf("Forward -->");
    space();
}

void back_function()
{
    printf("Back <--");
    space();
}

int main()
{
    space();

    // Make a new Audio struct named Player and pass in the function names to asign them to the struct pointers
    audio Player = {play_function, stop_function, pause_function, forward_function, back_function};
    Player.play_ptr();    // This calls the play function from the audio struct Player
    Player.forward_ptr(); // This calls the forward function from the audio struct Player
    Player.pause_ptr();   // This calls the pause function from the audio struct Player
    Player.play_ptr();    // This calls the play function from the audio struct Player
    Player.back_ptr();    // This calls the back function from the audio struct Player
    Player.stop_ptr();    // This calls the stop function from the audio struct Player
}
Dirty Moose

Pointer struct c

#include <stdio.h>
#include <stdlib.h>
struct person {
   int age;
   float weight;
   char name[30];
};

int main()
{
   struct person *ptr;
   int i, n;

   printf("Enter the number of persons: ");
   scanf("%d", &n);

   // allocating memory for n numbers of struct person
   ptr = (struct person*) malloc(n * sizeof(struct person));

   for(i = 0; i < n; ++i)
   {
       printf("Enter first name and age respectively: ");

       // To access members of 1st struct person,
       // ptr->name and ptr->age is used

       // To access members of 2nd struct person,
       // (ptr+1)->name and (ptr+1)->age is used
       scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
   }

   printf("Displaying Information:\n");
   for(i = 0; i < n; ++i)
       printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);

   return 0;
}
Evil Emu

Jawaban yang mirip dengan “C struct pointer berfungsi”

Pertanyaan yang mirip dengan “C struct pointer berfungsi”

Lebih banyak jawaban terkait untuk “C struct pointer berfungsi” di C

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya