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 -
#include <stdio.h>
-
#include <conio.h>
-
-
struct a {
-
char b;
-
struct b *bStruct;
-
} aStr = { '0' };
-
-
struct b {
-
int a;
-
int b;
-
int c;
-
} bStr = { 0, 0, 0 };
-
int main()
-
{
-
struct a Buf = { '0' };
-
-
Buf.bStruct->b = 1;
-
-
return 0;
-
}
-
15 1550
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 -
#include <stdio.h>
-
#include <conio.h>
-
-
struct a {
-
char b;
-
struct b *bStruct;
-
} aStr = { '0' };
-
-
struct b {
-
int a;
-
int b;
-
int c;
-
} bStr = { 0, 0, 0 };
-
int main()
-
{
-
struct a Buf = { '0' };
-
-
Buf.bStruct->b = 1;
-
-
return 0;
-
}
-
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
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: -
//replace this line
-
struct b *bStruct;
-
-
//with
-
struct b bStruct;
-
Thanks in advance!
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.
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: -
//replace this line
-
struct b *bStruct;
-
-
//with
-
struct b bStruct;
-
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
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 -
struct a {
-
int a;
-
int b;
-
struct b *ptrTob;
-
} str = { 0,0, malloc(sizeof(somesize))};
-
-
struct b {
-
int a;
-
int b;
-
};
-
So how is this done?
Oh for the second part, the compiler doesn't allow me to do -
struct a {
-
int a;
-
int b;
-
struct b bStruct;
-
};
-
-
struct b {
-
int a;
-
int b;
-
};
-
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!
I cant understand why you have to initialize the structure in the global scope.
Check this code...... -
#include <stdio.h>
-
//#include <conio.h>
-
-
struct a {
-
char b;
-
struct b *bStruct;
-
} aStr = { '0' };
-
-
struct b {
-
int a;
-
int b;
-
int c;
-
} bStr = { 0, 0, 0 };
-
int main()
-
{
-
//struct a Buf = { '0' };
-
struct a Buf;
-
Buf.b = '0';
-
Buf.bStruct = (struct b*)malloc(sizeof(struct b));
-
Buf.bStruct->b = 1;
-
-
printf("%d \n",Buf.bStruct->b);
-
-
return 0;
-
}
-
This works fine and i am able to set the value also for b.
Check it
Raghuram
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?
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.
.............................
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
Well still I don't understand why this segment doesn't work.. -
struct a {
-
int a,b;
-
} aStruct;
-
-
struct b {
-
struct a aStructInstnace;
-
int a,b;
-
} bStruct;
-
-
int main()
-
{
-
return 0;
-
}
-
Can any1 help me on this :p?
That compiles and runs on my computer, though you never initialize it....
What's your issue with that?
Well still I don't understand why this segment doesn't work.. -
struct a {
-
int a,b;
-
} aStruct;
-
-
struct b {
-
struct a aStructInstnace;
-
int a,b;
-
} bStruct;
-
-
int main()
-
{
-
return 0;
-
}
-
Can any1 help me on this :p?
Seeing anything strange in this line:
struct a aStructInstnace;
?
Seeing anything strange in this line:
struct a aStructInstnace;
?
Good catch, I guess I should have copied his code directly...
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 -
// add this and it fails
-
struct b InstnaceofB;
-
-
InstanceofB.aStructInstance.a = 9;
-
-
Well i wrote a segment that shows this: -
#include <stdio.h>
-
//#include <conio.h>
-
-
struct a {
-
char b;
-
struct b bStruct;
-
} aStr;
-
-
struct b {
-
int a;
-
int b;
-
int c;
-
} bStr;
-
int main()
-
{
-
-
struct a aStructInstnace;
-
-
aStructInstnace.bStruct.a = 9;
-
-
return 0;
-
}
-
it says it points to an unknown structure, dunno why.
Thanks in advance
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
by: Johs32 |
last post by:
I have the follwing code:
#include <stdio.h>
#include <stdlib.h>
struct data {
int *ip;
};
|
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...
|
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;
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
|
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...
| |