Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to know what really are the differences between these two pieces of code and what are any advantages of one over the one, if any. Can there be undefined behavior with either one of these? I would assume not because there are no local variables in the Add function.

1st code:

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

void Add(int a, int b, int *c)
 {
    *c = a + b;
 }

int main()
{
    int a = 2, b = 4, c;
    Add(a, b, &c);
    printf("Sum = %d\n", c);

    return 0;
}

2nd code:

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

int *Add(int a, int b, int *c)
 {
    *c = a + b;
    return c;
 }

int main()
{
    int a = 2, b = 4, c, *ptr;
    ptr = Add(a, b, &c);
    printf("Sum = %d\n", *ptr);

    return 0;
}
share|improve this question
    
Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. –  gnat 23 mins ago
    
When would you use a pointer to a value and when would you return a pointer to a value? –  unixpipe 10 mins ago
    
    
Of all the energy you wasted, wouldnt it have been easier to just answer the question? Or is your pride, ego and hate so high that you would rather spend more energy on making sure I dont get my answer?? –  unixpipe 1 min ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.