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

structs with fields that are structs

Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.

I am using shared memory and need to put everything in one struct.

Any help would be appreciated.
Nov 14 '05 #1
10 1951
Patricia Van Hise wrote:
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
I think you mean

struct subStr *lock;
};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.


Did you assign the address of a struct myStr to info? If so, did you
assign the address of a struct subStr to info->lock?

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #2
Patricia Van Hise wrote:
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
I think you mean

struct subStr *lock;
};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.


Did you assign the address of a struct myStr to info? If so, did you
assign the address of a struct subStr to info->lock?

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #3
Patricia Van Hise wrote:
struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
should be: struct subStr *lock;
does this compile? do not compile C code with a C++ compiler
};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.


How do you allocate info or lock? you did not provide the code, the
access of num1 is correct but most probably info or lock are not
pointing to a valid memory block, please provide the actual code you use.

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggraph.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #4
Patricia Van Hise wrote:
struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
should be: struct subStr *lock;
does this compile? do not compile C code with a C++ compiler
};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.


How do you allocate info or lock? you did not provide the code, the
access of num1 is correct but most probably info or lock are not
pointing to a valid memory block, please provide the actual code you use.

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggraph.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #5

"Patricia Van Hise" <ol**@aol.com> wrote in message
news:1e**************************@posting.google.c om...
Is it possible to access a field of a struct which is a field of
another struct?
Yes.

struct inner
{
int i;
int j;
};

struct outer
{
int x;
struct inner si;
};

struct outer out =
{
42,
{25, 99}
};

printf("%d\n", out.si.j); /* prints 99 */
Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
You have a syntax error. Should be:

struct subStr *lock;

BUT:
This element is not a struct object, it's a pointer to
a struct object.
};

struct myStr *info;
This is not a struct object, it's a pointer to a struct object.
I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.
You tried to store a value in memory not owned by your program.
You need to actually create a type 'myStr' object in order
to store data in it. And if you want to store anything in
a type 'subStr' object, you need to create on of those too.

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr lock;
};

struct myStr info = {0};
info.lock.num1 = 5;

I am using shared memory and need to put everything in one struct.


See above.
Why do you believe you need to use pointers?

-Mike
Nov 14 '05 #6

"Patricia Van Hise" <ol**@aol.com> wrote in message
news:1e**************************@posting.google.c om...
Is it possible to access a field of a struct which is a field of
another struct?
Yes.

struct inner
{
int i;
int j;
};

struct outer
{
int x;
struct inner si;
};

struct outer out =
{
42,
{25, 99}
};

printf("%d\n", out.si.j); /* prints 99 */
Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
You have a syntax error. Should be:

struct subStr *lock;

BUT:
This element is not a struct object, it's a pointer to
a struct object.
};

struct myStr *info;
This is not a struct object, it's a pointer to a struct object.
I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.
You tried to store a value in memory not owned by your program.
You need to actually create a type 'myStr' object in order
to store data in it. And if you want to store anything in
a type 'subStr' object, you need to create on of those too.

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr lock;
};

struct myStr info = {0};
info.lock.num1 = 5;

I am using shared memory and need to put everything in one struct.


See above.
Why do you believe you need to use pointers?

-Mike
Nov 14 '05 #7
On 4 Apr 2004 17:35:24 -0700, ol**@aol.com (Patricia Van Hise) wrote:

[Note: there's nothing here in the way of analysis that hasn't been already
posted, but perhaps you may find the complete program examples
useful...there weren't /any/ responses yet when I started working on this,
so I may as well not let the examples go to waste! ;-) ]
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
Either you've been compiling with a C++ compiler, or there's a typedef line
such as:
typedef struct subStr subStr;
you haven't shown us...

};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.
Sounds as if the value of lock within the struct myStr you're pointing to
with info was never initialized...or perhaps info itself wasn't
initialized, it is difficult to tell from what you've shown. If your
structs are all being allocated dynamically, the proper sequence of events
would look something like this:

#include <stdio.h>
#include <stdlib.h>

struct subStr{
int num1;
int num2;
};

struct myStr {
int num3;
struct subStr *lock;
};

int main()
{
struct myStr *info;

info = malloc(sizeof(struct myStr));
info->lock = malloc(sizeof(struct subStr));
/* ... */
info->lock->num1 = 5;
/* ... */
free(info->lock);
free(info);

return 0;
}


I am using shared memory and need to put everything in one struct.
Perhaps, then, you don't want to be using dynamic allocation after all, and
just want to be using nested structs:

#include <stdio.h>

struct subStr{
int num1;
int num2;
};

struct myStr {
int num3;
struct subStr lock;
};

int main()
{
struct myStr info;

/* ... */
info.lock.num1 = 5;
/* ... */

return 0;
}


Any help would be appreciated.

Hope that was some,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #8
On 4 Apr 2004 17:35:24 -0700, ol**@aol.com (Patricia Van Hise) wrote:

[Note: there's nothing here in the way of analysis that hasn't been already
posted, but perhaps you may find the complete program examples
useful...there weren't /any/ responses yet when I started working on this,
so I may as well not let the examples go to waste! ;-) ]
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;
Either you've been compiling with a C++ compiler, or there's a typedef line
such as:
typedef struct subStr subStr;
you haven't shown us...

};

struct myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.
Sounds as if the value of lock within the struct myStr you're pointing to
with info was never initialized...or perhaps info itself wasn't
initialized, it is difficult to tell from what you've shown. If your
structs are all being allocated dynamically, the proper sequence of events
would look something like this:

#include <stdio.h>
#include <stdlib.h>

struct subStr{
int num1;
int num2;
};

struct myStr {
int num3;
struct subStr *lock;
};

int main()
{
struct myStr *info;

info = malloc(sizeof(struct myStr));
info->lock = malloc(sizeof(struct subStr));
/* ... */
info->lock->num1 = 5;
/* ... */
free(info->lock);
free(info);

return 0;
}


I am using shared memory and need to put everything in one struct.
Perhaps, then, you don't want to be using dynamic allocation after all, and
just want to be using nested structs:

#include <stdio.h>

struct subStr{
int num1;
int num2;
};

struct myStr {
int num3;
struct subStr lock;
};

int main()
{
struct myStr info;

/* ... */
info.lock.num1 = 5;
/* ... */

return 0;
}


Any help would be appreciated.

Hope that was some,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #9


Russell Hanneken wrote:
Patricia Van Hise wrote:
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;

I think you mean

struct subStr *lock;
};
struct
myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.

Did you assign the address of a struct myStr to info? If so, did you
assign the address of a struct subStr to info->lock?


I suspect that the OP didn't and that is the source of the
seg fault.

The op can either define the struct with the substr object or leave
it a pointer and either allocated storage or have it point to
storage.

Sample 1:
#include <stdio.h>

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr lock;
};

int main(void)
{
struct myStr a = { 4,{5,6}};

printf("a.num3 = %d\n"
"a.lock.num1 = %d\n"
"a.lock.num2 = %d\n",
a.num3, a.lock.num1, a.lock.num2);
return 0;
}
Sample 2
#include <stdio.h>

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr *lock;
};

int main(void)
{
struct subStr sub = {5,6};
struct myStr a = {3};

a.lock = &sub;
printf("a.num3 = %d\n"
"a.lock->num1 = %d\n"
"a.lock->num2 = %d\n",
a.num3, a.lock->num1, a.lock->num2);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #10


Russell Hanneken wrote:
Patricia Van Hise wrote:
Is it possible to access a field of a struct which is a field of
another struct? Ex. struct subStr{
int num1;
int num2;
};
struct myStr {
int num3;
subStr *lock;

I think you mean

struct subStr *lock;
};
struct
myStr *info;

I tried to store a value in num1 with the instruction:

info->lock->num1 = 5;

and got a segmentation fault.

Did you assign the address of a struct myStr to info? If so, did you
assign the address of a struct subStr to info->lock?


I suspect that the OP didn't and that is the source of the
seg fault.

The op can either define the struct with the substr object or leave
it a pointer and either allocated storage or have it point to
storage.

Sample 1:
#include <stdio.h>

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr lock;
};

int main(void)
{
struct myStr a = { 4,{5,6}};

printf("a.num3 = %d\n"
"a.lock.num1 = %d\n"
"a.lock.num2 = %d\n",
a.num3, a.lock.num1, a.lock.num2);
return 0;
}
Sample 2
#include <stdio.h>

struct subStr
{
int num1;
int num2;
};

struct myStr
{
int num3;
struct subStr *lock;
};

int main(void)
{
struct subStr sub = {5,6};
struct myStr a = {3};

a.lock = &sub;
printf("a.num3 = %d\n"
"a.lock->num1 = %d\n"
"a.lock->num2 = %d\n",
a.num3, a.lock->num1, a.lock->num2);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #11

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

Similar topics

9
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne:...
19
by: Jasper Kent | last post by:
Can anyone explain the logic behind structs being allowed neither memeber initialisers or default constructors. Doesn't this just encourage developers to create constructors with dummy...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.