Search
 
SCRIPT & CODE EXAMPLE
 

C

c assign pointer to struct


// setting a pointer to a struct in C
// and set value by pointer
#include <stdio.h>
#include <stdbool.h>

// a struct called airplane
typedef struct
{
    int fuel;
    double speed;
    bool landing_gear_down;
} airplane;

int main()
{
    // make a pointer
    airplane *Hummingbird; // *Hummingbird is now a pointer and it can be assigned to point at a struct of type airplane.

    // this creates a struct of type airplane and assigns it a name of cessna and initializes it
    airplane cessna = {50, 155, true};

    Hummingbird = &cessna; // now assign the pointer to the struct address

    // you can now call data types / variables via the pointer
    (*Hummingbird).fuel; // this is a little messy
    Hummingbird->speed;  // this is clean and understandable

    printf("
the cessna has %d liters of fuel", Hummingbird->fuel);

    // set the value by pointer

    Hummingbird->fuel = 15;

    printf("
the cessna has %d liters of fuel", Hummingbird->fuel);

    (*Hummingbird).landing_gear_down = false;
   // or 
  	Hummingbird->landing_gear_down = false;

    if (Hummingbird->landing_gear_down == false)
    {

        printf("
landing gear is up");
    }
    else
    {

        printf("
landing gear is down");
    }
};
Comment

c struct pointer to function


//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("
");
}
// 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
}
Comment

struct pointer 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:
");
   for(i = 0; i < n; ++i)
       printf("Name: %s	Age: %d
", (ptr+i)->name, (ptr+i)->age);

   return 0;
}
Comment

return pointer to struct in C

typedef struct {
  char name[20];
  int age;
} Info;

Info* GetData() {
  Info *info;

  info = (Info *) malloc( sizeof(Info) );
  scanf("%s", info.name);
  scanf("%d", &info.age);

  return info;
}
Comment

C Pointers to struct

struct name {
    member1;
    member2;
    .
    .
};

int main()
{
    struct name *ptr, Harry;
}
Comment

PREVIOUS NEXT
Code Example
C :: Multi Select with icons htm; 
C :: c programming print pattern pyramid 
C :: how to alias an awk command 
C :: Uri/beecrowd problem no - 1099 solution in C 
C :: UTC offset upper limit 
C :: diiferent between * and & in c 
C :: elastic search url date 
C :: google sheets transpose new line to table 
C :: variadic macros c 
C :: how we can strore a nested structure values in arrays 
C :: unia c 
C :: changing data type in one line c program 
C :: python adding calculator 
C :: reap zombie process in c 
C :: resize vm boot disk with empty space 
C :: sdl close ev 
C :: reverse number in c 
C :: write to file in c programming 
C :: printf("%d", 10 ? 0 ? 5:1:1:12) what will print 
C :: C Variable Byte Sizes 
Dart :: flutter remove debug badge 
Dart :: flutter disbal PageView swipe 
Dart :: make a rounded container flutte 
Dart :: flutter snackbar circular shape rounded circle 
Dart :: flutter clear all text in textfield 
Dart :: date now dart 
Dart :: dart async vs async* 
Dart :: flutter compare dates 
Dart :: flutter display widget based on device orientation 
Dart :: flutter duration to string 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =