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