need help in structure assigning value from struct member to outside the structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aliaj00
    New Member
    • Jun 2013
    • 4

    #1

    need help in structure assigning value from struct member to outside the structure

    i have the code bellow, and i print the data but cannot assign it to global variable. any help with be much appreciated.

    Code:
    struct frequency_data
    {
     char frequency[10];
     int32 frequenca_mesatare;
     int16 PWM_F;
     int32 PR2;
     int16 Duty_Cycle;
    };
    void display(struct frequency_data FRQ)
    {
       printf(" Output\nName: %s",FRQ.frequency);
    }
    void main()
    {
    char koli[10];
    
    struct frequency_data *test;
       
       struct frequency_data s1;
       printf( "Enter Frequency:  ");
       gets(&s1.frequency);
       test =&s1;
        koli = test->frequency;does not work
        printf("koli eshte :   %s",test->frequency ); works
        koli = *test.frequency; does not work
        printf("koli eshte :   %s",koli );
        //test = (char*)s1.frequency;
        //printf ("-.....-: %s",test);
       //printf(" Output\nName: %s",&my_struct_pointer.frequency);
      //display(s1);
    }
    Last edited by Rabbit; Jun 20 '13, 04:58 PM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Think about this.

    koli is a char array in main(). Remember that the name of an array is the address of element 0. So koli is the address of koli[0].

    s1.frequency is a char array in s1. Again, the name of an array is the address of element 0. So s1.frequency is the address of s1.frequency[0].

    So your code:
    Code:
    koli = test->frequency;does not work
    is really trying to assign the address of the array
    s1.frequency[10] to the address of koli[10]. The compiler won't let you change the address of a local variable.

    Replace this line of code with a loop that copies the 10 elements of s1.frequency[10] to koli[10] one at a time:

    Code:
    for (int i = 0; i<10; ++i)
    {
         koli[i] = s1.frequency[i];
    }
    koli[i] is a char. s1.frequency[i] is a char. The code assigns a char to a char, which is always OK.

    Comment

    • aliaj00
      New Member
      • Jun 2013
      • 4

      #3
      thanks a lot i will try this one out :)

      BR,

      Aliaj00

      Comment

      • aliaj00
        New Member
        • Jun 2013
        • 4

        #4
        HI weaknessforcats

        thanks a lot it worked but i just assigned it like:

        Code:
        [B]koli[0] = test->frequency[/B]; //failing to get the value from test.frequency to koli if i comment it out the code works
              
            [B]printf("koli eshte :   %s",koli[0][/B] );
        and it worked.

        thanks again,

        BR,

        Aliaj00

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          There are two array syntaxes: a) brackets [] and b)pointers ->. You use brackets with the actual elements and you use pointers with the addresses of the elements:

          Code:
          struct data
          {
             int values[10];
          };
          
          int main()
          {
          data  stuff[10];
              stuff[2].values[5] = 2;   //use the actual stuff element
          
              (stuff + 2)->values[5] = 2;    //use the address of the stuff element
             
              
          }
          Using the array syntax ([...]) is preferred to the pointer syntax (->)

          Comment

          • aliaj00
            New Member
            • Jun 2013
            • 4

            #6
            thanks i will keep it in mind :).

            Comment

            Working...