C language: How to use STRUCT (typedef, “->”)


I was revising the material from first part of semester of my studies and decided to share it with you, maybe you will find it useful.

/* HOW TO USE STRUCTS, TYPEDEF */

#include <stdio.h>

/* struct is used in  C language to group data*/
/* This is how to use struct without typedef 
struct employee {
  char *first_name;
  char *last_name;
  int age;
  int emp_number;
  float salary;			
};
*/

/* Typedef makes struct much more readable !! */

typedef struct {
  char *first_name;
  char *last_name;
  int age;
  int emp_number;
  float salary;			
}Employee;




  char changeName( Employee *a){

	printf("\t\nChanging the name from ChangeName function to Tony\n");
	a->first_name ="Tony"; /* Suppose to change first name */
	/* -> is used to refer to the variable of another function in this case of a struct */
	
   }

 int main() {
   /* declaring struct  without typedef
  struct employee aspektors;
  */

   /*with typedef */

   Employee aspektors;

  aspektors.first_name = "Anatoly";
  aspektors.last_name="Spektors";
  aspektors.age=12;
  aspektors.emp_number=111222;
  aspektors.salary=20.5;


  /* printing  struct data */
  printf("\t\n Name: %s  \n Surname: %s  \n Age: %d \n Emp_number: %d \n Salary:%2.1f \n", aspektors.first_name, aspektors.last_name, aspektors.age, aspektors.emp_number, aspektors.salary);

   /* PASSING structure to function  by address to be able to change the variable in main*/
	changeName(&aspektors);

 printf(" \nAnd we changed the name to: %s using \n  \"->\" operator \n\n", aspektors.first_name);

	
   return 0;
  }

 

Have Fun!

Cheers!

About these ads

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module. Currently, I am employed as Software Engineer in Red Hat.
This entry was posted in C, Tutorials and tagged , , , , . Bookmark the permalink.

3 Responses to C language: How to use STRUCT (typedef, “->”)

  1. gonu says:

    poda mada panna appane

  2. Shishira S R says:

    data_size = recvfrom(sock_raw , buffer , 1024, 0 , &saddr ,
    (socklen_t*)&saddr_size);
    here is a recvfrom function, where i want to create a struct to ‘saddr’ and ‘saddr_size’ which is declared in the main() as follows

    struct sockaddr saddr;
    int saddr_size;
    saddr_size = sizeof saddr;
    and instead of buffer mentioned in the below write() api i have to use the struct variable.

    int cont= write(logfile,buffer,data_size);
    My question is: Below i have used structure.Is this the way to define struct for the following fields? Is it declared correctly? if not please somebody guide me to correct it.

    struct data{
    unsigned char buffer[1024];
    unsigned long int saddr;
    // struct sockaddr saddr;
    int saddr_size;
    };

  3. Shishira S R says:

    hello all,

    data_size = recvfrom(sock_raw , buffer , 1024, 0 , &saddr ,
    (socklen_t*)&saddr_size);
    here is a recvfrom function, where i want to create a struct to ‘saddr’ and ‘saddr_size’ which is declared in the main() as follows

    struct sockaddr saddr;
    int saddr_size;
    saddr_size = sizeof saddr;
    and instead of buffer mentioned in the below write() api i have to use the struct variable.

    int cont= write(logfile,buffer,data_size);
    My question is: Below i have used structure.Is this the way to define struct for the following fields? Is it declared correctly? if not please somebody guide me to correct it.

    struct data{
    unsigned char buffer[1024];
    unsigned long int saddr;
    // struct sockaddr saddr;
    int saddr_size;
    };

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s