structs with fields that are structs

  • Thread starter Patricia Van Hise
  • Start date
P

Patricia Van Hise

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.
 
R

Russell Hanneken

Patricia said:
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?
 
J

John Tsiombikas (Nuclear / the Lab)

Patricia said:
>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.
 
M

Mike Wahler

Patricia Van Hise said:
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
 
L

Leor Zolman

On 4 Apr 2004 17:35:24 -0700, (e-mail address removed) (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
 
A

Al Bowers

Russell said:
I think you mean

struct subStr *lock;



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;
}
 

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,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top