-2

So Im relatively new to c programming, in fact Im only taking this class as prerequisite to my major. So pointers are a rather foreign concept to me. For this specific assignment we are not allowed to use any refernce variables and must use memory allocation(malloc) in order to store values. For one of the parts however it asks to print the result of the product of 3 numbers(all assigned to an address by a pointer not a reference variable) my question is how do you multiply or really do any arithmetic using only pointers to point to the address of the values. I want to emphasize i do not want to multiply the address itself because im not sure that would help me. Again im very new to all this so any help would be greatly appreciated.

int main()
{
    int *a , *b, *c, *product;   
    product = malloc(sizeof(float));
    a = malloc(sizeof(int));
    b = malloc(sizeof(int));
    c = malloc(sizeof(int));
    *a = 1;
    *b = 2;
    *c = 3;
    *product = //this is where id assume itd go
} 

The assignment specifically says not to use reference variables so im not entirely sure how he wants us to go about it. Thanks.

Ryan2356
  • 1
  • 1
  • 3

1 Answers1

1

Its pretty much the same as a normal multiplication.

*product = (*a)*(*b)*(*c) //should do.
  • As you are using malloc do not forget to use stdlib.h file or else you will get warnings and it will be inefficient.

  • And do not use (*int) cast like these because the malloc returns *void and it leads to data corruption.

heeat
  • 128
  • 2
  • 9
  • 1
    *and it will be inefficient* -- ? Inefficient in terms of what and how? – Ajay Brahmakshatriya Feb 18 '19 at 05:46
  • 1
    *and it leads to data corruption* -- casting `void*` to `int*` leads to data corruption? How? – Ajay Brahmakshatriya Feb 18 '19 at 05:47
  • @AjayBrahmakshatriya Don't burn me if i am wrong this. malloc is included in stdlib.h header file, and if we don't include it in the program it will give warnings like warning: incompatible implicit declaration of built-in function 'malloc' even though its just a warning and he successfully compiles this program it is hiding the fact that he missed the header file and it may effect the program in latter stages so i feel that it is inefficient.Sorry if i am wrong. – heeat Feb 18 '19 at 05:49
  • yes you are 100% right about the warnings. But it wouldn't lead to any inefficiencies. Yes, OP should include `stdlib.h`. You are also correct about not needing a cast to `(int*)` after malloc, but it does not lead to any data corruption. – Ajay Brahmakshatriya Feb 18 '19 at 05:50
  • 1
    What you suggested is not wrong, just the reasons you gave for it are inaccurate. – Ajay Brahmakshatriya Feb 18 '19 at 05:51
  • @AjayBrahmakshatriya Compilers takes malloc is a function returning int, because there is no stdlib.h in the program but we know the void* pointer is what actually returned by malloc. int and pointers may take up different numbers of bytes in different machines ,so this type conv may lead to data corruption is what i meant. – heeat Feb 18 '19 at 06:09
  • @AjayBrahmakshatriya I'm pretty sure that allocate a float size for a int pointer could lead to unpredictable behavior. Please don't restart this debate, vote my friend ;) https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. – Stargateur Feb 18 '19 at 06:54
  • @heeat "Compilers takes malloc is a function returning int, because there is no stdlib.h in the program" not since C99 but compiler continue to follow this stupid rule https://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc/ – Stargateur Feb 18 '19 at 06:55
  • @Stargateur Understood.Thanks for the link. – heeat Feb 18 '19 at 08:24
  • @AjayBrahmakshatriya Thanks for your i will keep in my mind to be in-detail next time i answer any question. – heeat Feb 18 '19 at 08:33
  • @heeat I think I misunderstood what you had written. You meant casting `int` (default return type when there is no prototype) to `(int*)`. I thought you meant `void*` to `int*`. Yes, casting `int` to `int*` can lead to unexpected behaviors including data corruption. I will upvote your answer. – Ajay Brahmakshatriya Feb 18 '19 at 09:28
  • @Stargateur I am totally for not casting the result of malloc :) I think I misinterpreted heeat's answer. – Ajay Brahmakshatriya Feb 18 '19 at 09:29