explanation needed on assigning a struct member to an another structure ...

This is a discussion on explanation needed on assigning a struct member to an another structure ... within the C Programming forums, part of the General Programming Boards category; Guys, Code: (slaveIdent*) (SPIBuff.data); Could not able to figure out how i am able to assign a struct member to ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    35

    explanation needed on assigning a struct member to an another structure ...

    Guys,

    Code:
    (slaveIdent*) (SPIBuff.data);
    Could not able to figure out how i am able to assign a struct member to an another structure in the main function... can someone explain the above declaration in words....

    Full code

    Code:
    #include <stdio.h>
    #include <string.h> // for strcmp()
    #include <stdbool.h> // C99 feature
    typedef struct {
        char    neuron_id [15];
        char id_string [15];
    } slaveIdent;
    typedef struct {
        char transLen;
        int transType;
        char data[32];
    } sSpiBuf;
    
    /* The actual transaction buffer                                     */
    sSpiBuf  SPIBuff;
    
    int main(void)
    {
    
    (slaveIdent*) (SPIBuff.data);
    
    return 0;
    }
    I stripped the code ... actually they are using such declaration in memcpy...as below
    Code:
    memcpy(((slaveIdent *)SPIBuff.data)->id_string,idstr,15);
    Please shed some light....

    Thanks n regards
    max

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    It basically allows you to pretend that you have this.
    Code:
    typedef struct {
        char    neuron_id [15];
        char id_string [15];
    } slaveIdent;
    typedef struct {
        char transLen;
        int transType;
        // char data[32];
        slaveIdent data;
    } sSpiBuf;
    SPIBuff.data (being an array) is equivalent to &SPIBuff.data[0]
    Knowing that it is a pointer, then (slaveIdent *) is a cast to another pointer type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Hi salem, Thank you for the response.... can you please shed some more light.. still i got few questions....

    Q1)
    if this the case
    Code:
    typedef struct {
        char transLen;
        int transType;
        // char data[32];
        slaveIdent data;
    } sSpiBuf;
    then why doesnt this work printf("content : %s",SPIBuff.data->neuron_id); what i mean is that i am not able to print individual members .
    q2)
    what i understood from this (slaveIdent *) is that the data stored in SPIBuff.data is pointed by some random slaveIdent pointer..... is it so..??
    Code:
    #include <stdio.h>
    #include <string.h> // for strcmp()
    #include <stdbool.h> // C99 feature
    typedef struct {
        char    neuron_id [15];
        char id_string [15];
    } slaveIdent;
    typedef struct {
        char transLen;
        int transType;
        char data[32];
    } sSpiBuf;
    
    /* The actual transaction buffer                                     */
    sSpiBuf  SPIBuff;
    
    int main(void)
    {
    char   nd[15] = "jack as a cat";
     char   idstr[15] = "sky is blue";
    memcpy(((slaveIdent *)SPIBuff.data)->neuron_id,nd, 15);
    memcpy(((slaveIdent *)SPIBuff.data)->id_string,idstr, 15);
    printf("content : %s",SPIBuff.data);
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It works perfectly. But to print a string using %s, the char array is expected to terminate with a nul. You should therefore do
    Code:
    SPIBuff.data[30] = '\0';
    after the memcpy statements.

    > what i understood from this (slaveIdent *) is that the data stored in SPIBuff.data is pointed by some random slaveIdent pointer..... is it so..??

    No. You cast one data type to another. There is no new pointer created. The compiler understands the named members of the cast structure as offsets to use with the SPIBuff.data area.
    Last edited by nonoob; 10-04-2012 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bus error when assigning value to member of struct
    By popapez in forum C Programming
    Replies: 4
    Last Post: 09-23-2009, 08:37 PM
  2. Tic Tac Toe Explanation Needed
    By almister in forum C Programming
    Replies: 0
    Last Post: 08-28-2009, 12:10 PM
  3. A bit of explanation needed.
    By JacobN in forum C Programming
    Replies: 3
    Last Post: 11-24-2007, 04:41 PM
  4. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 04:53 PM
  5. Explanation needed, what is this?
    By CAP in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 04:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21