473,320 Members | 1,939 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,320 software developers and data experts.

Wierd Struct thingy

Andr3w
42
Hi guys, merry x-mas to ya all folks!

Let me get straight to the point, why the following code just fails to run :p

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. struct a {
  5.     char b;
  6.     struct b *bStruct;
  7. } aStr = { '0' };
  8.  
  9. struct b {
  10.     int a;
  11.     int b;
  12.     int c;
  13. } bStr = { 0, 0, 0 };
  14. int main()
  15. {
  16.     struct a Buf = { '0' };
  17.  
  18.     Buf.bStruct->b = 1;
  19.  
  20.     return 0;
  21. }
  22.  
Dec 27 '07 #1
15 1550
gpraghuram
1,275 Expert 1GB
Hi guys, merry x-mas to ya all folks!

Let me get straight to the point, why the following code just fails to run :p

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. struct a {
  5.     char b;
  6.     struct b *bStruct;
  7. } aStr = { '0' };
  8.  
  9. struct b {
  10.     int a;
  11.     int b;
  12.     int c;
  13. } bStr = { 0, 0, 0 };
  14. int main()
  15. {
  16.     struct a Buf = { '0' };
  17.  
  18.     Buf.bStruct->b = 1;
  19.  
  20.     return 0;
  21. }
  22.  

Hi,
Where r u allocating memory to the member variable bStruct?
Without allocating memory u are trying to assign a value to it, thats why it is crashing

Raghuram
Dec 27 '07 #2
Andr3w
42
can you refrom the above code with the allocation it needs in order to work, please?

Also why does this again fail (this time even to compile) if I don't use a pointer

i mean:
Expand|Select|Wrap|Line Numbers
  1. //replace this line
  2. struct b *bStruct;
  3.  
  4. //with
  5. struct b bStruct;
  6.  
Thanks in advance!
Dec 27 '07 #3
sicarie
4,677 Expert Mod 4TB
can you refrom the above code with the allocation it needs in order to work, please?
And is there a reason you can't reform your code to work?

Why don't you give it a try and post what you get. If you can't get something to work, we'll point you in the right direction.
Dec 27 '07 #4
gpraghuram
1,275 Expert 1GB
can you refrom the above code with the allocation it needs in order to work, please?

Also why does this again fail (this time even to compile) if I don't use a pointer

i mean:
Expand|Select|Wrap|Line Numbers
  1. //replace this line
  2. struct b *bStruct;
  3.  
  4. //with
  5. struct b bStruct;
  6.  
Thanks in advance!
If u are using stack objects you cant use -> operator and you have to use . operator.
Have you changed the code after changing the variable type.
As suggested by sicarie please try it and get back

Raghuram
Dec 27 '07 #5
Andr3w
42
I don't know what's missing from the first one that's why I am asking because I don't know where I have to allocate this memory, because obviously I can't use malloc in the struct like

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2. int a;
  3. int b;
  4.     struct b *ptrTob;
  5. } str = { 0,0, malloc(sizeof(somesize))};
  6.  
  7. struct b {
  8.     int a;
  9.     int b;
  10. };
  11.  
So how is this done?

Oh for the second part, the compiler doesn't allow me to do
Expand|Select|Wrap|Line Numbers
  1. struct a {
  2. int a;
  3. int b;
  4.     struct b bStruct;
  5. };
  6.  
  7. struct b {
  8.     int a;
  9.     int b;
  10. };
  11.  
it says I have undefined symbol b but I have it already defined. It doesn't matter if I place the definition of b above or below struct a but it just doesn't work, dunno again why.

Thanks in advance!
Dec 27 '07 #6
gpraghuram
1,275 Expert 1GB
I cant understand why you have to initialize the structure in the global scope.
Check this code......
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. //#include <conio.h>
  3.  
  4. struct a {
  5.     char b;
  6.     struct b *bStruct;
  7. } aStr = { '0' };
  8.  
  9. struct b {
  10.     int a;
  11.     int b;
  12.     int c;
  13. } bStr = { 0, 0, 0 };
  14. int main()
  15. {
  16.     //struct a Buf = { '0' };
  17.     struct a Buf;
  18.     Buf.b = '0';
  19.     Buf.bStruct = (struct b*)malloc(sizeof(struct b));
  20.     Buf.bStruct->b = 1;
  21.  
  22.     printf("%d \n",Buf.bStruct->b);
  23.  
  24.     return 0;
  25. }
  26.  
This works fine and i am able to set the value also for b.


Check it

Raghuram
Dec 27 '07 #7
Andr3w
42
Hi, thanks for the reply! One thing I don't understand since I use malloc to allocate some memory don't I have to free it too?
Dec 27 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
Hi, thanks for the reply! One thing I don't understand since I use malloc to allocate some memory don't I have to free it too?
Yes, you do.
.............................
Dec 27 '07 #9
gpraghuram
1,275 Expert 1GB
Hi, thanks for the reply! One thing I don't understand since I use malloc to allocate some memory don't I have to free it too?

Thanks for pointing it out.
Raghuram
Dec 28 '07 #10
Andr3w
42
Well still I don't understand why this segment doesn't work..

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2. int a,b;
  3. } aStruct;
  4.  
  5. struct b {
  6.   struct a aStructInstnace;
  7.   int a,b;
  8. } bStruct;
  9.  
  10. int main()
  11. {
  12.  return 0;
  13. }
  14.  
Can any1 help me on this :p?
Dec 30 '07 #11
sicarie
4,677 Expert Mod 4TB
That compiles and runs on my computer, though you never initialize it....

What's your issue with that?
Dec 30 '07 #12
Savage
1,764 Expert 1GB
Well still I don't understand why this segment doesn't work..

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2. int a,b;
  3. } aStruct;
  4.  
  5. struct b {
  6.   struct a aStructInstnace;
  7.   int a,b;
  8. } bStruct;
  9.  
  10. int main()
  11. {
  12.  return 0;
  13. }
  14.  
Can any1 help me on this :p?


Seeing anything strange in this line:

struct a aStructInstnace;

?
Dec 30 '07 #13
sicarie
4,677 Expert Mod 4TB
Seeing anything strange in this line:

struct a aStructInstnace;

?
Good catch, I guess I should have copied his code directly...
Dec 30 '07 #14
Andr3w
42
well not excatly is, that is not what i meant. That alone compiles what it doesn;t compile is if you add the following lines to main

Expand|Select|Wrap|Line Numbers
  1. // add this and it fails
  2. struct b InstnaceofB;
  3.  
  4. InstanceofB.aStructInstance.a = 9;
  5.  
  6.  

Well i wrote a segment that shows this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. //#include <conio.h>
  3.  
  4. struct a {
  5.     char b;
  6.     struct b bStruct;
  7. } aStr;
  8.  
  9. struct b {
  10.     int a;
  11.     int b;
  12.     int c;
  13. } bStr;
  14. int main()
  15. {
  16.  
  17.     struct a aStructInstnace;
  18.  
  19.     aStructInstnace.bStruct.a = 9;
  20.  
  21.     return 0;
  22. }
  23.  
it says it points to an unknown structure, dunno why.

Thanks in advance
Dec 31 '07 #15
gpraghuram
1,275 Expert 1GB
The error comes as the struct b is not defined when u are declaring the struct b inside struct a.
If it is a pointer declaration it wont create any issues.
Since it is a stack object it needs to know the size , thats why you are getting the error.

Thanks
Raghuram
Dec 31 '07 #16

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

Similar topics

3
by: Markus Fischer | last post by:
Hi, I'm experiencing a wierd problem with IE 6 in Windows with two _slightly_ different Version. Give the following HTMl-Code, ideally the output of offsetTop should be "105"; a few pixel...
3
by: Sathyaish | last post by:
A practice excercise from K&R. Kindly read the comments within the program. I'd be very grateful to people who helped. Why is it that I get the wierd face-like characters on the screen instead of...
3
by: B Vidyadhar Joshi | last post by:
I was writing an Bluetooth Application which makes calls to Windows APIs. I feel I'm doing something wrong with the structure. Could somebody help me? The code that I'm using is pasted below: ...
0
by: Michael | last post by:
Hi, I found a wierd problem in DataGrid. If I set DataGrid's DataSource to empDataSet1 at designtime, then I can never change its DataSource at runtime, e.g., in the Button1_Click event...
0
by: Tom | last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my case, file names). I normally load this via a loop, adding each item to the list box in the loop. I put lb.BeginUpdate...
5
by: subaruwrx88011 | last post by:
I am very new to c++ and very new to maps. Below is the source of my program. The output is very strange. Am I inserting correctly into the map? Any help will be greatly appreciated. #include...
5
by: Johs32 | last post by:
I have the follwing code: #include <stdio.h> #include <stdlib.h> struct data { int *ip; };
5
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
2
by: Cromulent | last post by:
Okay here is the question, what is wrong with this? typedef struct nntpServerInfo { char login; char password; char serverName; int port; } nntpServerInfo;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.