The following code gives me a Compile error "incompatible types at assignment"
File 1:
struct x{
int a;
int b;
int c;
};
File 2:
static struct x d;
void copyStructVal(){
d-> a = 1;
d-> b = 2;
d-> c = 3;
}
x getStruct(){
copyStructVal();
return d;
}
File 3:
static struct x e;
void copy(){
e = getStruct();
}
I've searched for this and can't find the answer. Can I do it with a Pointer? (I'm a amateur in C)
In C, you need to write struct behind the name of a structure, unless you typedef it. In other words:
x getStruct(){
Must be:
struct x getStruct(){
Since you wrote it in the rest of the code, I guess it is a typo.
On top of that, you have to fix these 3 lines, since d is not a pointer:
d-> a = 1;
d-> b = 2;
d-> c = 3;
They should be:
d.a = 1;
d.b = 2;
d.c = 3;
Related
He folks,
i got a problem and a question.
Hopefully u can help and explain me.
first of all i have 2 stucts:
typedef struct {
double x;
double y;
} A;
typedef struct {
unsigned int count;
A(*stack)[];
}B;
this struct B i declare in main() and passing a Pointer of B to a function this will initializ
main(){
B x;
function rr(&x);
}
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
on this line
(test->stack+ i)->x= 89;
compiler says incomplete Type
i know why it is incomplete cause in struct B their is no dimension.
but array should initialize in function rr
Maybe u understand what i mean and how to solve my problem.
function rr i am not allowed to change.
Greetings
EDIT 1
Thank you for all answers
mabey i schould clearify my problem
typedef struct {
unsigned int count;
A(*stack)[]; // here i want a pointer to an array of A's
}B;
//over main it is declared
void rr(B*);
main(){
B x;
function rr(&x);
}
// this func is not allowed to change
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89; // error cause incomplete type but i
//have to use this line
}
}
Hope now it is easier to i understand what i want
This declaration:
A(*stack)[];
Says that stack is a pointer to an array of A of unknown size. That is an incomplete type which means it can't be used directly.
It seems like what you actually want is not a pointer to an array, but a pointer to the first member of a dynamic array of A. So declare the member as a pointer:
A *stack;
In the expression:
(test->stack+ i)->x= 89;
before accessing an array via a pointer to an array you must dereference it.
Try:
(*test->stack)[i].x= 89;
You do not know how to use flexible array members.
Simply:
typedef struct {
double x;
double y;
} A;
typedef struct {
size_t count;
A stack[];
}B;
B *createStack(size_t size)
{
B *s = malloc(sizeof(*s) + size * sizeof( s -> stack[0]));
return s;
}
void rr(B* test) {
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
int main(void)
{
B *stack = createStack(4);
rr(stack);
free(stack);
}
You need only one allocation to mallloc/realloc or free the structure. The array will decay into pointer for your assignment in rr function.
I have a larger piece of code (I didn't write it) that makes use of complex numbers defined through a structure. I need to edit it and simply multiply a complex number by a real but cant seem to get it right. I keep getting the following error message.
error: invalid operands to binary * (have ‘cplx’ and ‘double’)
I know this could be done using the complex.h library but that would mean a lot of rewriting so is there a simpler way? The code below reproduces my problem.
#include <stdio.h>
#include <math.h>
typedef struct cplxS {
double re;
double im;
} cplx;
int main()
{
double a = 1.3;
cplx b = {1, 2};
c = a * b;
}
You will first have to initialize a node using malloc
#include <stdlib.c>
int main(){
double a = 1.3;
//initialize struct
struct cplxS* b = malloc(sizeof(struct cplxS));
//set values for b
b->re = 1;
b->im = 2;
//preform your multiplication
double c = a*(b->re); //value c with re
double d = a*(b->im); //value d with im
//free node memory
free(b);
b = NULL;
}
If you want to update the struct by multiplying c to its values, it would follow
#include <stdlib.c>
int main(){
double a = 1.3;
//initialize struct
struct cplxS* b = malloc(sizeof(struct cplxS));
//set values for b
b->re = 1;
b->im = 2;
//preform your multiplication
b->re = a*(b->re); //update b with a*re
b->im = a*(b->im); //value b with a*im
//free node memory
free(b);
b = NULL;
}
Hope this helps!
You need to either cast the double as a complex number, and use the complex multiplication function (which presumably exists, or could/should be written by overloading * operator)
int main()
{
double a = 1.3;
cplx a_c = {1.3, 0}
cplx b = {1, 2};
c = a_c * b;
}
or actually perform the multiplication, either by building a definition for multiplication of reals and complex numbers (not shown), or just doing it yourself.
int main()
{
double a = 1.3;
cplx b = {1, 2};
cplx c = {b.re*a, b.im*a};
}
#define MAX 100
struct bs{
int ab;
int ac;
}be;
struct s{
be b;
int c;
int d;
int e;
}fe;
int a[MAX];
fe f;
Technique 1:
f.b.ab = a;
memset(&a,0,sizeof(a));
f.b.ac = MAX;
Technique 2:
f.b.ab = a;
f.b.ac = MAX;
memset(&a,0,sizeof(a));
Technique 3:
memset(&a,0,sizeof(a));
f.b.ab = a;
f.b.ac = MAX;
which is the best technique to follow and why?
Technique 3:
memset(&a,0,sizeof(a));
f.b.ab = a;
f.b.ac = MAX;
because both a and fb.b.ab will have clear memories with only one memset() call. Any other kind of optimization you might been hoping for is insignificant.
None of the above. The best would be:
int a[MAX] = { 0 };
fe f = { .b.ab = a, .b.ac = MAX };
None. What you probably mean for a start is memset(a,0,sizeof(a));
a is an array that is converted to a pointer in that context, no need to take the address. (The C FAQ has more information about arrays and pointers.)
Then for the rest of it you should revise your basic knowledge of C before you go further
struct s{
be b;
int c;
int d;
int e;
}fe;
fe f;
Here the structure type is struct s and fe is a variable of that type. So the last line here makes no sense at all.
#include<stdio.h>
#include<stdlib.h>
struct test{
char b;
int a;
int c ;
};
int main()
{
struct test inst;
struct test *ptr_test = &inst;
char * ptr_ch;
int* ptr_i;
/*ptr_ch = (char *) ptr_test;*/
ptr_ch = (char*)ptr_test;
ptr_i = (int *) ptr_test;
*ptr_ch = 'b';
*ptr_i = 13;
printf("char b = %c, int a = %d int c = %d", inst.b, inst.a, inst.c);
return 0;
}
I expected the output to give the appropriate values of a,b and garbage value of c.
But on the terminal, if I do ./a.out the output is:
, int a = 134513785 int c = 13173540
When I do $./a.out > tmp; vim tmp, the output is:
char b = ^M, int a = 134513785 int c = 12714788
What is the problem?
I wanted to access individual fields of the struct using typecasting.
for instance, I wanted to know another way to return the value of &(inst.a).
Your pointers
ptr_ch = (char*)ptr_test;
ptr_i = (int *) ptr_test;
do not automatically refer to the first apropriate member variable (in your case b and a). Rather they just reinterpret the pointer to the structure as pointer to char or int, so they point to the same location, the address of your structure. With the char you might be lucky that it's the first member and you are really pointing to the char, but your int pointer points to the same address and therefore overwrites it with platform- and compiler-dependent garbage.
So don't do those kinds of things (or do them when you really know what you are doing and, more important, where (on what platform and with what compiler) you are doing it).
ptr_ch and ptr_i point to the same memory location:
ptr_ch = (char*)ptr_test;
ptr_i = (int *) ptr_test;
when you do the following you are reading from the same memory address:
*ptr_ch = 'b'; //write the first sizeof(char) byte of the structure ptr_test
*ptr_i = 13; //write the first sizeof(int) bytes of the structure ptr_test overwriting some bytes wrote before
You should eventually do something like:
ptr_ch = &(ptr_test->b);
*ptr_ch = 'b';
ptr_i = &(ptr_test->a);
*ptr_i = 13;
Why would you use the typecasting? Why not do the following:
ptr_ch = &(ptr_test->b);
ptr_i = &(ptr_test->a);
13 is decimal for an ASCII carriage return - when you do *ptr_i = 13;, you're setting b to that 13. If you change your print out to look like:
printf("char b = %c (%d), int a = %d int c = %d", inst.b, inst.b, inst.a, inst.c);
You'll see that you get:
(13), int a = 1 int c = 1606416024
as output instead. The carriage return character is causing your char b output to get overwritten by the output following the carriage return character. It might be more obvious if you used a different number than 13. For example, using 86, you'll get:
char b = V, int a = 1 int c = 1606416024
as the output. The reason a and c don't make sense is because they're uninitialized.
You can't just typecast a structure pointer to a pointer of another type and expect the compiler to extract a pointer to a field inside that structure for you - that's not how it works. I think you might have been trying to do this:
ptr_ch = &ptr_test->b;
ptr_i = &ptr_test->a;
Here's a complete example program that does what I think you're trying for:
#include<stdio.h>
struct test {
char b;
int a;
int c;
};
int main(void)
{
struct test inst = {0, 0, 0};
struct test *ptr_test = &inst;
char *ptr_ch;
int *ptr_i;
ptr_ch = &ptr_test->b;
ptr_i = &ptr_test->a;
*ptr_ch = 'b';
*ptr_i = 86;
printf("char b = %c, int a = %d int c = %d\n", inst.b, inst.a, inst.c);
return 0;
}
And its output:
char b = b, int a = 86 int c = 0
You should use the strict conforming offsetof macro, which calculates the offset of any struct element from struct begin:
ptr_ch = (char*)ptr_test;
*ptr_ch = 'b';
ptr_i = ptr_ch + offsetof(struct test,a);
*ptr_i = 13;
ptr_i = ptr_ch + offsetof(struct test,c);
*ptr_i = 14;
for example:
typedef struct {
int num;
int den;
} rational_number;
rational_number a;
What is the difference between using
a.num or a.den
and
a->num or a->den
Thx in advance.
-> is for accessing the members of a pointer to a struct, whereas . is for accessing members of the struct itself. a->num is really just shorthand for (*a).num. Example:
typedef struct {
int num;
int den;
} rational_number;
rational_number a;
r.num = 1;
rational_number *a_ptr = &a;
a_ptr->num = 2; /* a.num is now 2 */
you use a.num, when it is normal variable, and a->num when it is pointer
For ex.
rational_number a;
a.num; // is correct
a->num; // is wrong
rational_number *b;
b.num;// wrong
b->num; //is correct
The difference is that in the first case you access the structure via a stack variable:
rational_number a;
a.num = 1;
a.den = 1;
in the second case via a heap pointer:
rational_number* a = new rational_number();
a->num = 1;
a->den = 1;
If a were declared as a pointer to your structure, a->num would return the value of num.
rational_number *a;
a->num = 5;
int new_a;
new_a = a->num;
You have a is declared as a structure, so you should use a.num to return the value of num.
rational_number a;
a.num = 5;
int new_a;
new_a = a.num;
Both set the value of new_a to 5.
Also, you can get the address of a if it is a structure and use it like a pointer.
rational_number a;
(&a)->num = 5;
int new_a;
new_a = a.num;