structure pointers

R

raghu

Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;
t.c = 31;
t.q[0].d =21;
t.q[0].e = 23;
t.q[1].d = 11;
t.q[1].e = 4;


t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are ");
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n", t.q[0].d);
printf(" %d \n",t.T->ucType);
return 0;
}
when I run this program some error is generating that is Segmentation
fault.
Can you please help me.
Thanks in advance.

-Bye
Raghu
 
R

Richard Heathfield

raghu said:
Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;

Here's your problem. You can't have executable code except inside a
function.
t.c = 31;

This breaks for the same reason.
t.q[0].d =21;

This breaks for the same reason, but also because t.q doesn't point anywhere
useful (or if it was statically initialised, it has the value NULL). Either
way, t.q[0] is an illegal memory access.
 
R

raghu

Richard said:
raghu said:
Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;

Here's your problem. You can't have executable code except inside a
function.
t.c = 31;

This breaks for the same reason.
t.q[0].d =21;

This breaks for the same reason, but also because t.q doesn't point anywhere
useful (or if it was statically initialised, it has the value NULL). Either
way, t.q[0] is an illegal memory access.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

But even I modified the code as follows the error is repeting.
the code is
main(){
struct temp t;
t.a = 4;
t.c = 31;


t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are %d\n ", sizeof(t));
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n",t.T->ucType);
return 0;


}
the structure defination is same as above. By the by I am using gcc
compiler.
 
S

Steven Kirby

raghu said:
Richard said:
raghu said:
Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;
Here's your problem. You can't have executable code except inside a
function.
t.c = 31;
This breaks for the same reason.
t.q[0].d =21;
This breaks for the same reason, but also because t.q doesn't point anywhere
useful (or if it was statically initialised, it has the value NULL). Either
way, t.q[0] is an illegal memory access.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

But even I modified the code as follows the error is repeting.
the code is
main(){
struct temp t;
t.a = 4;
t.c = 31;


t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are %d\n ", sizeof(t));
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n",t.T->ucType);
return 0;


}
the structure defination is same as above. By the by I am using gcc
compiler.

In the temp struct definition, you define T as a pointer to a tlv
structure, but you never actually assign it to one. You're trying to use
an undefined pointer, which is most likely why it's crashing. Either you
need to assign the T pointer to a tlv struct, or just declare T as a tlv
struct variable, not a pointer.
 
R

raghu

Steven said:
raghu said:
Richard said:
raghu said:

Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;
Here's your problem. You can't have executable code except inside a
function.

t.c = 31;
This breaks for the same reason.

t.q[0].d =21;
This breaks for the same reason, but also because t.q doesn't point anywhere
useful (or if it was statically initialised, it has the value NULL). Either
way, t.q[0] is an illegal memory access.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

But even I modified the code as follows the error is repeting.
the code is
main(){
struct temp t;
t.a = 4;
t.c = 31;


t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are %d\n ", sizeof(t));
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n",t.T->ucType);
return 0;


}
the structure defination is same as above. By the by I am using gcc
compiler.

In the temp struct definition, you define T as a pointer to a tlv
structure, but you never actually assign it to one. You're trying to use
an undefined pointer, which is most likely why it's crashing. Either you
need to assign the T pointer to a tlv struct, or just declare T as a tlv
struct variable, not a pointer.

Hello,

Thank Q very much. Your idea worked. I want to know which C
compiler is the best one.
please inform me.
once again Thank Q
Bye
Raghu
 
S

Steven Kirby

raghu said:
Steven said:
raghu said:
Richard Heathfield wrote:
raghu said:

Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;
Here's your problem. You can't have executable code except inside a
function.

t.c = 31;
This breaks for the same reason.

t.q[0].d =21;
This breaks for the same reason, but also because t.q doesn't point anywhere
useful (or if it was statically initialised, it has the value NULL). Either
way, t.q[0] is an illegal memory access.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
But even I modified the code as follows the error is repeting.
the code is
main(){
struct temp t;
t.a = 4;
t.c = 31;


t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are %d\n ", sizeof(t));
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n",t.T->ucType);
return 0;


}
the structure defination is same as above. By the by I am using gcc
compiler.
In the temp struct definition, you define T as a pointer to a tlv
structure, but you never actually assign it to one. You're trying to use
an undefined pointer, which is most likely why it's crashing. Either you
need to assign the T pointer to a tlv struct, or just declare T as a tlv
struct variable, not a pointer.

Hello,

Thank Q very much. Your idea worked. I want to know which C
compiler is the best one.
please inform me.
once again Thank Q
Bye
Raghu

That's pretty much a matter of opinion. Personally, I use MinGW (a
Windows port of the GCC compiler, and some other tools) for console and
SDL programming, but I find Visual C++ to be much handier for Windows
programming.

I'm not sure what you're using at the moment, but Dev-C++ is a simple
and practical environment for Windows which comes bundled with MinGW.
You can get it from http://www.bloodshed.net/devcpp.html
 
F

Fred Kleinschmidt

raghu said:
Hello

This is Raghu. I have a problem in the pointers.
The code is
typedef struct Tlv{
/*type Length value for each
message */
unsigned char ucType; /*Type varies from message to
message */
unsigned short usLength;/*Length */
unsigned int ucValue;/*Value*/
}tlv;


typedef struct te{
char d;
int e;
}Q;
struct temp{
int a;
unsigned int c;
Q *q;
tlv *T;
};
struct temp t;
t.a = 4;
t.c = 31;
t.q[0].d =21;
t.q[0].e = 23;
t.q[1].d = 11;
t.q[1].e = 4;
Another problem here. What is t.q?
You have not assigned q to point anywhere
t.T -> ucType = 1;
t.T -> usLength = 11;
t.T -> ucValue = 21;
printf("The entered values are ");
printf("%d\n%d\n", t.a, t.c);
printf(" %d \n", t.q[0].d);
printf(" %d \n",t.T->ucType);
return 0;
}
when I run this program some error is generating that is Segmentation
fault.
Can you please help me.
Thanks in advance.

-Bye
Raghu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top