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.

Have Fun!

Cheers!

About the author

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

View all posts

2 Comments

  • 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;
    };

  • 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

Your email address will not be published. Required fields are marked *