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

structure pointers

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

Oct 30 '06 #1
7 1900
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)
Oct 30 '06 #2

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.

Oct 30 '06 #3
raghu wrote:
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.
Oct 30 '06 #4

Steven Kirby wrote:
raghu wrote:
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

Oct 30 '06 #5
raghu said:
>
I want to know which C
compiler is the best one.
Define "best".

--
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)
Oct 30 '06 #6
raghu wrote:
Steven Kirby wrote:
>raghu wrote:
>>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
Oct 30 '06 #7

"raghu" <ra*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
2
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
2
by: Vince | last post by:
I have a very specific problem to solve but I cannot find a data structure for it. I don't know if I am posting on the good newsgroup but I cannot find a software.design group. I would like to...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
6
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. ...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
9
by: nano2 | last post by:
Hi , Have the following case passing in structure pointers and returning structure pointers can anyone spot whats wrong with this structure name is structCar void callfn( ){
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.