Answers for "pointer to a struct"

C++
1

how to assign struct address to the pointer

#include<stdio.h>

struct dog
{
    char name[10];
    char breed[10];
    int age;
    char color[10];
};

int main()
{
    struct dog my_dog = {"tyke", "Bulldog", 5, "white"};
    struct dog *ptr_dog;
    ptr_dog = &my_dog;

    printf("Dog's name: %sn", ptr_dog->name);
    printf("Dog's breed: %sn", ptr_dog->breed);
    printf("Dog's age: %dn", ptr_dog->age);
    printf("Dog's color: %sn", ptr_dog->color);

    // changing the name of dog from tyke to jack
    strcpy(ptr_dog->name, "jack");

    // increasing age of dog by 1 year
    ptr_dog->age++;

    printf("Dog's new name is: %sn", ptr_dog->name);
    printf("Dog's age is: %dn", ptr_dog->age);

    // signal to operating system program ran fine
    return 0;
}
Posted by: Guest on July-24-2020
1

how to use pointer to struct c++

typedef struct{
    int vin;
    char* make;
    char* model;
    int year;
    double fee;
}car;

car TempCar;
tempCar->vin = 1234;  // metodo 1
(*tempCar).make = "GM"; // metodo 2
tempCar->year = 1999;
tempCar->fee = 20.5;
Posted by: Guest on April-10-2021
1

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;
}
Posted by: Guest on May-15-2021

Code answers related to "pointer to a struct"

Browse Popular Code Answers by Language