Paiku Han
Paiku Han

Reputation: 583

Trouble of pointers as function arguments

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

Upvotes: 0

Views: 59

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

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;
}

Upvotes: 2

Deduplicator
Deduplicator

Reputation: 45704

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.

Upvotes: 1

ravi
ravi

Reputation: 10733

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.

Upvotes: 1

Related Questions