Reputation: 23
recently i started writing apps with c language. i have a problem with sum operator in below code. when i start program and i give for ex. 4 to int a; and 6 to int b; and then i want a+b witch give me 22. here is code, first i define my structure:
struct student {
int a;
int *b;
}*s1;
then i scan the values and the other steps:
int sum, x, y;
s1 = malloc(sizeof(struct student));
scanf("%d", &x);
scanf("%d", &y);
s1->a = x;
s1->b = y;
sum = (s1->a) + (s1->b);
printf("SUM: %d", sum);
Also i test *(s1->a) and still have problem. Thanks.
IMPORTANT: i don't want the other ways that i can solve this problem or suggesting the better ways of coding this algorithm, i just want to know why this piece of code not working properly and how can i fix it. Actually i want keep the 'b' as a pointer!
Upvotes: 0
Views: 2540
Reputation: 20244
int *b
in your structure is a pointer not an int
. To fix your code,you can assign the address of y
to the pointer like this:
s1->a = x;
s1->b = &y;
sum = (s1->a) + *(s1->b); //asterisk dereferences the pointer and gets the value stored in the memory address
printf("SUM: %d", sum);
Or just declare b
as an int
variable.
Upvotes: 0
Reputation: 44043
As @Mitch Wheat mentioned in the comments,
struct student {
int a;
int *b; // <-- this is a pointer, not an int
}*s1;
This has the effect that in the expression
(s1->a) + (s1->b);
pointer arithmetic is used, and the expression has the same value as &s1->b[s1->a]
would have if s1->b
were a valid pointer (that it isn't is a problem that is not triggered in the code).
Since s1->a == 4
and s1->b == memory address 6
, the numerical value of (s1->a) + (s1->b)
then is 6 + sizeof(int) * 4
. If sizeof(int) == 4
, this becomes 22
, and that is the behavior you get.
The solution is to make b
an int:
struct student {
int a;
int b;
}*s1;
Note that this is just a description of the behavior you get, not of behavior you are guaranteed to get. On a platform where pointers have a different size from int
s, for example, the printf
will not work as expected because the %d
format specified does not use the whole pointer value.
Upvotes: 1
Reputation: 25129
Your problem is in the definition of the structure and/or how you are using it:
struct student {
int a;
int *b;
}*s1;
Here a
is an int
but b
is a pointer to an int
. If x
had that struct type, you'd need to use x.a
but *(x.b)
to refer to the integer. So if s
is a pointer to the struct, you need to use s->a
but *(s->b)
.
However, you are using s1
as a pointer to the struct, but trying to use s->a
and s->b
seemingly both as integers. That won't work because of the way you have declared the struct.
More precisely:
s1->a = x;
s1->b = y;
sum = (s1->a) + (s1->b);
printf("SUM: %d", sum);
That sum line is doing pointer arithmetic (as s1->b
is a pointer). This is unlikely to be what you required. Remove the *
from the definition, i.e. use:
struct student {
int a;
int b;
} *s1;
You don't need to change the way you malloc()
the struct. The fact that s1
is a pointer to the struct is a separate issue from whether the struct contains a pointer.
Upvotes: 0