Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:34:39+00:00 2026-06-14T21:34:39+00:00

I learn now about sockets and I find it very confusing because the casting

  • 0

I learn now about sockets and I find it very confusing because the casting structres.

The guide say to include 8 bit inside the sockaddr_in to compare it to sockaddr.

So my question is why to do it I mean when you cast you dont compare the char size to int

For example you do

char a[1]='1';
int b=(int)a;

and not

char a[2]='1';//compare to size of int
int b=(int)a; 

so how is it working?

Is it diiferent when its castig strucres?
If yes so why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:34:41+00:00Added an answer on June 14, 2026 at 9:34 pm

    When passing pointers to structures, the function that receives it may try to access any of all its fields.

    If you receive a struct something *, you expect that you can read any of the sizeof(struct something) bytes following the pointer you receive. So, not reserving those bytes in your own struct would make them incompatible – whenever the function tries to access the bytes you haven’t allocated, it would be accessing non-reserved memory, so it can be a segmentation fault, or can corrupt another’s structure data.


    Look at this program:

    #include <stdlib.h>
    #include <stdio.h>
    
    struct __attribute__ ((__packed__)) pair {
            short first;
            char second;
            long int third;
            int forth;
            char last;
    };
    
    void main(void) {
            struct pair myPair;
            printf("myPair         is at 0x%x\n", &myPair);
            printf("myPair.first   is at 0x%x\n", &(myPair.first));
            printf("myPair.second  is at 0x%x\n", &(myPair.second));
            printf("myPair.third   is at 0x%x\n", &(myPair.third));
            printf("myPair.forth   is at 0x%x\n", &(myPair.forth));
            printf("myPair.last    is at 0x%x\n", &(myPair.last));
    }
    

    And a sample output:

    myPair         is at 0xabbd0aa0
    myPair.first   is at 0xabbd0aa0
    myPair.second  is at 0xabbd0aa2
    myPair.third   is at 0xabbd0aa3
    myPair.forth   is at 0xabbd0aab
    myPair.last    is at 0xabbd0aaf
    

    What we’ve learnt here is that each field is stored next to the previous one in memory, more precisely sizeof(previous_field) bytes to the right of the previous field (when struct is packed – see this for understanding why packed, but this is the ideal case).

    So, imagine we would like to create another struct to be compatible with this one. If we create something like:

    struct __attribute__ ((__packed__)) small_pair {
            long int first;
            char second;
            int third;
            char forth;
    };
    

    We can pass a struct small_pair * to any function that expects a struct pair * by casting:

    void my_function(struct pair *);
    
    void main(void) {
        struct small_pair my_small_pair;
        // ...
        my_function((struct pair*) &my_small_pair);
        // ...
    }
    
    void my_function(struct pair *a_pair) {
        //...
        printf("Second character of pair is %c\n", a_pair->second);
        //...
        printf("Last character of pair is %c\n", a_pair->last);
        //...
    }
    

    Once compiled, accessing a_pair->second is “read the one byte which is two bytes after the start of the struct” (0xabbd0aa2 - 0xabbd0aa0 = 2). So that would be the third byte of the field first of a struct small_pair, whichever value it has.

    But, what about a_pair->last? It’s 0xf (15) bytes after the struct’s start, but it’s clearly out of it’s space (sizeof(struct small_pair) is just 14).

    So it will depend on the way variables got loaded in memory, but clearly we will not be referring to the value we wanted. The best case would be when that address is out of our process space, so we get a Segmentation Fault, and the program aborts. But it could well be that there’s another variable declared in that position of memory, and we will be reading/writing a different variable from what we wanted, leaving to who-knows-what results.

    So, if we just add another 2-bytes-long field to the end of the struct small_pair, we guarantee that every possible reference of a struct pair will still be correct in our own struct, so they’ll be compatible at memory-level.

    Then, it’s still left the semantic-level compatibility, but that’s a different story 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learn now about sockets and I saw that there is two struct Sockaddr
I just started to learn about mod rewrite process. Now, I got some problems
Ok guys, I started to learn more about Jquery and now I could make
I'm attempting to learn a little more about handling sockets and network connections in
In my attempt to learn core JavaScript, I am now learning about EventListeners. My
I've been trying to learn about Neural Networks for a while now, and I
To learn a bit more about FreeBSD and *nix systems in general, I'm starting
I've been trying to learn about Neural Networks for a while now, and I
I am thinking about to learn new language or framework. Now I deal with
I'm trying to learn something about Optimizing and indexes because I ran a insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.