0

I have created a function that does the euclidean division in C and putting the quotient and remainder as function arguments.

code listing 1:

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

void euclidean(int a, int b, int * q, int * r);

int main(int argc, char ** argv){

    int * q;
    int * r;

    euclidean(5,4, q, r);

    printf("q = %d, r = %d", *q, *r);

    return 0;
}

void euclidean(int a, int b, int * q, int * r){
    q = (int*)malloc(sizeof(int));
    r = (int*)malloc(sizeof(int));

    *q = a/b;
    *r = a % b;

    //printf("q = %d, r = %d", *q, *r); //this will show

    return;
}

code listing 2:

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

void euclidean(int a, int b, int * q, int * r);

int main(int argc, char ** argv){

    int * q;
    int * r;

    euclidean(5,4, q, r);

    printf("q = %d, r = %d", *q, *r);

    return 0;
}


void euclidean(int a, int b, int * q, int * r)
{
  *q = a / b;
  *r = a % b;

  //printf("q = %d, r = %d", *q, *r); //this won't show 

  return;
}

Both version aren't working. I compiled and ran the code on windows and the program is killed on runtime without printing anything (i.e. "q = 1, r = 4"). And my guess is if I had to compile and run it on linux, the terminal would gave me a "segmentation fault" error (not sure). I really don't see why it isn't working, especially with code listing 1. For code listing 2 one can argue that since the result of the operation are some sort of constant variable inside a function in which they were created, they could not be kept at the end of the function (I need confirmation on that too). Thanks

chris
  • 60,560
  • 13
  • 143
  • 205
Paiku Han
  • 581
  • 2
  • 16
  • 38
  • 2
    In C and C++ function arguments are passed by value. `q` and `r` are uninitialized in `main`. – juanchopanza Nov 02 '14 at 20:46
  • Start with adding `free` – deviantfan Nov 02 '14 at 20:47
  • As an aside, [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845). Also, a `void`-function really does not need an explicit `return;` at the end. Also, since C99 `main` has an implicit `return 0;` at the end. And if you have the choice, consider putting the whole function, instead of a forward-declaration, before all its call-sites. If you don't repeat yourself, there are fewer chances for bugs. – Deduplicator Nov 02 '14 at 21:02
  • Read wikipage on [closures](http://en.wikipedia.org/wiki/Closure_%28computer_programming%29). – Basile Starynkevitch Nov 02 '14 at 21:07
  • these lines: int * q; int * r; are not correct, they should be: int q: int r: then the call to euclidean() in main should use &q and &r – user3629249 Nov 03 '14 at 02:59

3 Answers3

2

Keep It Simple, Stupid (there is such a principle).:)

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

void euclidean(int a, int b, int * q, int * r);

int main(int argc, char ** argv){

    int q;
    int r;

    euclidean(5,4, &q, &r);

    printf("q = %d, r = %d", q, r);

    return 0;
}

void euclidean(int a, int b, int * q, int * r)
{
  *q = a / b;
  *r = a % b;
}

If you want indeed to allocate memory in the function then the code will look like

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

void euclidean(int a, int b, int * *q, int * *r);

int main(int argc, char ** argv){

    int * q;
    int * r;

    euclidean(5,4, &q, &r);

    printf("q = %d, r = %d", *q, *r);

    free( q );
    free( r );

    return 0;
}

void euclidean(int a, int b, int * *q, int * *r){
    *q = (int*)malloc(sizeof(int));
    *r = (int*)malloc(sizeof(int));

    **q = a/b;
    **r = a % b;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

What you want is known as pass-by-reference, which allows the callee to modify the object the caller provides.

As C is purely pass-by-value, that is simulated with pointers:

The caller passes the address of the object the callee should modify, and the callee dereferences the received pointer to modify the intended target.

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

void euclidean(int a, int b, int * q, int * r)
{
  // Modify the target-objects q and r point to
  *q = a / b;
  *r = a % b;
}

int main() {
    int q, r; // Not pointers, the objects themselves!
    euclidean(5, 4, &q, &r); // Passing the addresses of q and r
    printf("q = %d, r = %d", q, r);
}

No dynamic allocation at all.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
1

You are not getting values for listing_2 because:-

void euclidean(int a, int b, int * q, int * r)
{
  *q = a / b;      <<<<<<<<<<<<<<<<<
  *r = a % b;

  //printf("q = %d, r = %d", *q, *r); //this won't show 

  return;
}

Neither you have allocated memory for q,r in main nor in euclidean. You were getting error as you are trying to deference a pointer not initialized to anything.

ravi
  • 10,994
  • 1
  • 18
  • 36