error: request for member "machine" in something not a structure or union - pointers

When I'm trying to compile below program I'm getting error as request for member "machine" in something not a structure or union
struct machine
{
int a;
int b;
int c;
};
struct config
{
struct machine *machine;
int n;
};
int main()
{
struct config *conf;
struct machine *mach;
mach->a=1;
mach->b=2;
mach->c=3;
conf.machine=mach; /* error in this line */
return 0;
}
Can anyone help me in fixing this bug.. Thanks in advance!!!

conf is a pointer, not a structure, so you have to dereference it, just like you did with mach.
conf->machine = mach;
Also, you need to allocate memory for both conf and mach.

Related

memcpy to copy local array of struct into local array of struct

In C language, I want to copy 1 local array of structure into other local array of structure. Sample code example below. Which is the correct way to use memcpy() in this case?
'#define CAPACITY 5
int main(void)
{
typedef struct Prototype
{
int value;
} Prototype;
int i;
Prototype vProto1[CAPACITY], vProto2[CAPACITY];
for (i=0; i<CAPACITY; i++)
{
vProto1[i].value = i+1;
}
Here I want to copy vProto1 into vProto2. Is below command correct?
memcpy(vProto2, vProto1, sizeof(Prototype) * CAPACITY);
OR this is correct?
memcpy(&vProto2, &vProto1, sizeof(Prototype) * CAPACITY);
Thanks in advance. I am confused whether I should use "&" or not.

passing different structure type in c for functions

I have a following code :
typedef struct PStruct{
int len;
char* data;
}PointerStruct;
typedef struct AStruct{
int len;
char data[256];
}ArrayStruct;
void checkFunc(PointerStruct* myData)
{
if (0 == myData || 0 == myData->data){
printf("error\n");
}
}
int main()
{
ArrayStruct my_data;
my_data.len = 256;
char data[] = "data is sent";
my_data.data = &data;
checkFunc((PointerStruct*)my_data);
return 0;
}
is there any wrong in passing structure which has array. where as the required is pointer.
please let me know.
There are a couple of points to be considered in your program.
char data[] = "data is sent";
This is a character array of 13 characters. Hence, my_data.data = &data; will give a compilation error as shown below
error: incompatible types when assigning to type 'char[256]' from type 'char (*)[13]'
To copy your string, you could probably use strcpy as shown below
strcpy(my_data.data, data);
Next point is passing the pointer to the object. In this call, checkFunc((PointerStruct*)my_data);, you are passing the instance of the object to the function call, but are type-casting as a pointer. You would face compilation issues due to the mismatch of the datatypes as error: cannot convert to a pointer type
To overcome this error, you should pass a reference to your my_data object as checkFunc((PointerStruct*) &my_data);. Hence, your new main function would look like
int main()
{
ArrayStruct my_data;
my_data.len = 256;
char data[] = "data is sent";
//my_data.data = &data;
strcpy(my_data.data, data); // Use of strcpy. You would require to include <string.h>
checkFunc((PointerStruct*)(&my_data)); // Pass a reference and not by value
return 0;
}
With these changes, your code should work fine.

Pointers to stack

I am sorry that I cannot support my question with some code (I didnt understand how to structure it so it would be accepted here), but I try anyway.
If I understand correctly, a struct that references a struct of same type would need to do this with contained pointer for reference. Can this pointer reference to allocated space on the stack (instead of the heap) without creating segmentation fault? -
how should this be declared?
Yes, you can use pointers to variables on the stack, but only when the method that provides that stack frame has not returned. For example this will work:
typedef struct
{
int a;
float b;
} s;
void printStruct(const s *s)
{
printf("a=%d, b=%f\n", s->a, s->b);
}
void test()
{
s s;
s.a = 12;
s.b = 34.5f;
printStruct(&s);
}
This will cause an error however, as the stack frame would have disappeared:
s *bad()
{
s s;
s.a = 12;
s.b = 34.5f;
return &s;
}
EDIT: Well I say it will cause an error, but while calling that code with:
int main()
{
test();
s *s = bad();
printStruct(s);
return 0;
}
I get a warning during compilation:
s.c:27:5: warning: function returns address of local variable [enabled by default]
and the program appears to work fine:
$ ./s
a=12, b=34.500000
a=12, b=34.500000
But it is, in fact, broken.
You didn't say what language you are working in, so assuming C for now from the wording of your question... the following code is perfectly valid:
typedef struct str_t_tag {
int foo;
int bar;
struct str_t_tag *pNext;
} str_t;
str_t str1;
str_t str2;
str1.pNext = &str2;
In this example both str1 and str2 are on the stack, but this would also work if either or both were on the heap. The only thing you need to be careful of is that stack variables will be zapped when they go out of scope, so if you had dynamically allocated str1 and passed it back out of a function, you would not want str1->pNext to point to something that was on the stack within that function.
In other words, DON'T DO THIS:
typedef struct str_t_tag {
int foo;
int bar;
struct str_t_tag *pNext;
} str_t;
str_t *func(void)
{
str_t *pStr1 = malloc(sizeof(*pStr1));
str_t str2;
pStr1->pNext = &str2;
return pStr1; /* NO!! pStr1->pNext will point to invalid memory after this */
}
Not sure if this is specifically a C/C++ question, but I'll give C/C++ code as example in anyway.
The only way you can declare it: (with minor variations)
typedef struct abc
{
struct abc *other;
} abc;
other can point to an object on the stack as follows:
abc a, b; // stack objects
b.other = &a;
This is not a question about scope, so I'll skip commenting on possible issues with doing the above.
If, however, you want to assign it to a dynamically created object, there's no way this object can be on the stack.
abc b;
b.other = malloc(sizeof(abc)); // on the heap

gcc: /home/jamie/aws/btree_int.c|28|error: request for member ‘btree_start’ in something not a structure or union|

This code:
#include <stdlib.h>
#include <stdio.h>
int j_btree_create (int fn_initial_nodes);
typedef struct {
int depth;
int value;
void *item;
void *left_pointer;
void *right_pointer;
} j_btree_node_int;
typedef struct {
int nodes;
int available_nodes;
int btree_extension;
} j_btree_descriptor_int;
int j_btree_create (int fn_initial_nodes) {
int *free_btree_node;
int loop_counter;
j_btree_descriptor_int *btree_start;
btree_start = (j_btree_descriptor_int *) malloc (((sizeof(j_btree_node_int) + sizeof(free_btree_node)) * fn_initial_nodes) + sizeof(j_btree_descriptor_int));
printf ("btree_start: " . btree_start);
/* *btree_start.nodes = fn_initial_nodes;
*btree_start.available_nodes = fn_initial_nodes;
*btree_start.extension = NULL; */
for (loop_counter = 0; loop_counter < fn_initial_nodes; loop_counter++) {
printf ("loop_test:" . loop_counter);
}
}
Produces this error:
/home/jamie/aws/btree_int.c||In function ‘j_btree_create’:|
/home/jamie/aws/btree_int.c|28|error: request for member ‘btree_start’ in something not a structure or union|
/home/jamie/aws/btree_int.c|33|error: request for member ‘loop_counter’ in something not a structure or union|
||=== Build finished: 2 errors, 0 warnings ===|
When compiled with CodeBlocks. I have not managed to find an exact answer to my problem (I have looked), does anyone know roughly what I am doing wrong? Probably more than one thing given I am fairly new to C.
printf ("btree_start: " . btree_start);
This is not how the things are done in c. There's no . concatenation operator and you do not concatenate strings (pointers to characters) and pointers to structures. If you want to print out the pointer, it's
printf("btree_start: %p\n",btree_start);
For the loop counter it's
printf("loop_test: %d",loop_counter);

Pointers in structure

I'm trying to use a file pointer that I have declared in a structure of linked list, but I keep getting it as a NULL value.
I have the following structure:
struct _hash_table
{
char found;
struct _hash_chain *hash_chain;
}
struct _hash_chain
{
uint64_t value;
FILE *fout;
struct _hash_chain *next;
}
and
struct _hash_table hash_table[TABLE_SIZE];
I keep getting hash_table[i]->hash_chain->fout = NULL and it's pointer address is nil.
Do I need to dynamically allocate memory for the pointer?
struct _hash_table hash_table[TABLE_SIZE]; - This will not allocate memory for struct _hash_chain because hash_chain is pointer variable in _hash_table.
...
struct _hash_table hash_table[TABLE_SIZE];
for (i = 0; i < TABLE_SIZE; i++);
{
hash_table[i].hash_chain = (struct _hash_chain *)malloc(sizeof(struct _hash_chain));
memset(hash_table[i].hash_chain, 0, sizeof(struct _hash_chain));
}
//Then do file open for TABLE_SIZE times
//hash_table[0].hash_chain->fout = fopen("file.txt", "w");
...
Accssing h_table[i].hash_chain without dynamic memory allocation will leads to crash(an undefined behaviour). I hope you will take care of next pointer.

Resources