0

i have the following struct:

typedef struct number
{
    int x;
    int y;
    int z;
    unsigned long int final;
}number;

my code is the following:

number* numbers;
numbers= (number*)malloc(sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, &numbers->y, &numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d",numbers->final);

but the output is wrong. for example here is a run:

12 12 12
input: 12,12,12
final: -28640

i cant figure out the problem. the highest number that number->final can get is 90,000 (i make sure of it as i gives the input)... i seems like there is overlap? please help.

  • 4
    `final` is `unsigned` and you use `%d` in `printf` which is for signed `int`s. – Kiril Kirov Nov 09 '13 at 11:46
  • The code copy pasted does not relate to the output, please paste your real code. – ouah Nov 09 '13 at 11:47
  • i would have if i could use print screen but i can not as the site wont let me. – user106453 Nov 09 '13 at 11:48
  • 1
    copy/paste your real code, there's inconsistencies in what you have above (e.g. printf line mixes values and pointers). Make sure you turn on your compiler's warnings to maximum levels. – Mat Nov 09 '13 at 11:50

2 Answers2

1

Your problem is the pointer. I am assuming you initialised the struct as follows.

numbers *numbers;

However if you use it in the main where you declare it don't use a pointer. There are also a few errors in your printf call, you are printing the memory address of y and z instead of the value like you did for the x value.

Use something like this.

#include <stdio.h>

typedef struct number
{
    int x;
    int y;
    int z;
    unsigned int final;
} number;

int main()
{
  number numbers;

  scanf("%d %d %d", &numbers.x, &numbers.y, &numbers.z);
  printf("input: %d,%d,%d\n",numbers.x, numbers.y, numbers.z);
  numbers.final=(numbers.x)*4000 + (numbers.y)*50 + (numbers.z);
  printf("final: %d\n",numbers.final);

  return 0;
}

Right and if you used malloc it looks like this.

#include <stdio.h>
#include <stdlib.h>

typedef struct number
{
    int x;
    int y;
    int z;
    unsigned int final;
} number;

int main()
{
  number *numbers = malloc(1 * sizeof(number));

  scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
  printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);
  numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
  printf("final: %d\n",numbers->final);

  free(numbers);

  return 0;
}

Running example here

Montaldo
  • 863
  • 8
  • 16
  • in that case it's similar. – Montaldo Nov 09 '13 at 12:01
  • what did u change? i dont see any changes – user106453 Nov 09 '13 at 12:06
  • chech `printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);` line, the `&` are removed – abasu Nov 09 '13 at 12:11
  • Yes, don't cast your malloc either, the compiler will determine that. – Montaldo Nov 09 '13 at 12:13
  • @Montaldo In my opinion you should define main as `int main(void)` I know that in case of main it doesn't make huge difference but in order to create in good habit to add `void` to no argument functions. Please check additionally following question [http://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid/12225214#12225214] – Lazureus Nov 09 '13 at 13:38
  • I think that's a matter of style tbh, I never put void in an empty parameter list, i.e. default constructors and getters – Montaldo Nov 09 '13 at 15:30
  • @Montaldo are you talking about C++, because if yes then you are right, there is no difference how it is written with or without `void`, but in C we got situation when you can define function as i.e. `int test_function()` and then you can pass arguments to that function without any compiler warnings if you don't have e.g.`-Wstrict-prototypes` option in gcc turned on. Check this question below: http://stackoverflow.com/questions/13319492/understanding-the-difference-between-f-and-fvoid-in-c-and-c-once-and-for-a/13319578#13319578 – Lazureus Nov 09 '13 at 19:36
-1

The reason for your wrong answer is because you have kept the datatype as int which has max value of 32767 change it to unsigned long int as your ans calculates to 2400612

5eeker
  • 1,016
  • 1
  • 9
  • 30