473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. struct frequency_data
  2. {
  3.  char frequency[10];
  4.  int32 frequenca_mesatare;
  5.  int16 PWM_F;
  6.  int32 PR2;
  7.  int16 Duty_Cycle;
  8. };
  9. void display(struct frequency_data FRQ)
  10. {
  11.    printf(" Output\nName: %s",FRQ.frequency);
  12. }
  13. void main()
  14. {
  15. char koli[10];
  16.  
  17. struct frequency_data *test;
  18.  
  19.    struct frequency_data s1;
  20.    printf( "Enter Frequency:  ");
  21.    gets(&s1.frequency);
  22.    test =&s1;
  23.     koli = test->frequency;does not work
  24.     printf("koli eshte :   %s",test->frequency ); works
  25.     koli = *test.frequency; does not work
  26.     printf("koli eshte :   %s",koli );
  27.     //test = (char*)s1.frequency;
  28.     //printf ("-.....-: %s",test);
  29.    //printf(" Output\nName: %s",&my_struct_pointer.frequency);
  30.   //display(s1);
  31. }
Jun 20 '13 #1
5 1333
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i<10; ++i)
  2. {
  3.      koli[i] = s1.frequency[i];
  4. }
koli[i] is a char. s1.frequency[i] is a char. The code assigns a char to a char, which is always OK.
Jun 20 '13 #2
thanks a lot i will try this one out :)

BR,

Aliaj00
Jun 26 '13 #3
HI weaknessforcats

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

Expand|Select|Wrap|Line Numbers
  1. koli[0] = test->frequency; //failing to get the value from test.frequency to koli if i comment it out the code works
  2.  
  3.     printf("koli eshte :   %s",koli[0] );
and it worked.

thanks again,

BR,

Aliaj00
Jun 26 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. struct data
  2. {
  3.    int values[10];
  4. };
  5.  
  6. int main()
  7. {
  8. data  stuff[10];
  9.     stuff[2].values[5] = 2;   //use the actual stuff element
  10.  
  11.     (stuff + 2)->values[5] = 2;    //use the address of the stuff element
  12.  
  13.  
  14. }
Using the array syntax ([...]) is preferred to the pointer syntax (->)
Jun 26 '13 #5
thanks i will keep it in mind :).
Jun 27 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: junky_fellow | last post by:
I am trying to print the offset of a particulat member in a structure, but it's not working. I am using the following expression to print the offset. &(struct my_struct *)0->member_name ...
2
by: hilmilho | last post by:
Hi you, I'm facing a strange problem, and I don't know what to do. Maybe someone could help me understand this? I'm storing IP and Netmask on the struct below. 1st I store IP an print it, its...
10
by: googler | last post by:
Hello, I have a very simple question that I'm a bit confused about right now. Is it OK to allocate memory by malloc() or calloc() for a struct member and then call free() on it? For example, I have...
1
by: John | last post by:
Hi, What is the struct member aligment of a C# program? Are there any issues with P/Invoke when passing structs to 3rd party C dlls? Thanks
0
by: Urs Vogel | last post by:
Hi when setting the struct member alignment of a mixed code project to 'default', what is the actual alignment? I need to know since we're creating (struct-) data dynamically and pass it to a...
6
by: sieg1974 | last post by:
Hi, Given a pointer to a member within a struct, is it possible to determine the pointer to the structure? If so, how can it be done? Thanks, Andre struct Structure
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
1
by: dragonslayer008 | last post by:
1. Can a value struct inherit from a native struct? 2. Suppose I have a native structure: struct Vector3 { float x, y, z; }; and you make a corresponding managed version
2
by: saadkhan | last post by:
//I need to save float values in my structure which consist of three variables v1, v2 and v3. typedef struct { float v1; float v2; float v3; }Sil;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.