Trouble of pointers as function arguments

Paiku Han

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

Vlad from Moscow

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pointers as function arguments in C

From Dev

Giving names to the arguments in function pointers?

From Dev

Trouble with pointers and pointers to pointers

From Dev

Passing string into function and modifying in C (trouble with pointers)

From Dev

Trouble with method that takes in a function and its arguments

From Dev

Trouble passing arguments through an initialiser function in javascript

From Dev

Trouble with method that takes in a function and its arguments

From Dev

Having trouble understanding function arguments in Python

From Dev

Is using pointers and references of an object as function arguments lighter?

From Dev

Passing function pointers with generic arguments in D

From Dev

How are arguments passed to function pointers in C?

From Dev

trouble storing pointers in an array of pointers

From Dev

Is it undefined behaviour to call a function with pointers to different elements of a union as arguments?

From Dev

Call templated function with derived class arguments using base class pointers

From Dev

How to call a function that has as arguments an array of pointers? in C

From Dev

Using C++11 smart pointers as C function arguments

From Dev

Calling a function from a map of pointers to functions having a variable number arguments

From Dev

How to call a function that has as arguments an array of pointers? in C

From Dev

C++: Vector of function pointers to functions with different type of arguments

From Dev

C -- Trouble with pointers

From Dev

Having trouble using pointers

From Dev

Trouble with a recursive algorithm and pointers

From Dev

Having trouble with pointers

From Dev

Trouble with pointers and stack implementation

From Dev

Having a little trouble with pointers

From Dev

Having trouble with C pointers

From Dev

pass pointers to pointers to function

From Dev

pass pointers to pointers to function

From Dev

Passing by reference arguments and pointers

Related Related

  1. 1

    Pointers as function arguments in C

  2. 2

    Giving names to the arguments in function pointers?

  3. 3

    Trouble with pointers and pointers to pointers

  4. 4

    Passing string into function and modifying in C (trouble with pointers)

  5. 5

    Trouble with method that takes in a function and its arguments

  6. 6

    Trouble passing arguments through an initialiser function in javascript

  7. 7

    Trouble with method that takes in a function and its arguments

  8. 8

    Having trouble understanding function arguments in Python

  9. 9

    Is using pointers and references of an object as function arguments lighter?

  10. 10

    Passing function pointers with generic arguments in D

  11. 11

    How are arguments passed to function pointers in C?

  12. 12

    trouble storing pointers in an array of pointers

  13. 13

    Is it undefined behaviour to call a function with pointers to different elements of a union as arguments?

  14. 14

    Call templated function with derived class arguments using base class pointers

  15. 15

    How to call a function that has as arguments an array of pointers? in C

  16. 16

    Using C++11 smart pointers as C function arguments

  17. 17

    Calling a function from a map of pointers to functions having a variable number arguments

  18. 18

    How to call a function that has as arguments an array of pointers? in C

  19. 19

    C++: Vector of function pointers to functions with different type of arguments

  20. 20

    C -- Trouble with pointers

  21. 21

    Having trouble using pointers

  22. 22

    Trouble with a recursive algorithm and pointers

  23. 23

    Having trouble with pointers

  24. 24

    Trouble with pointers and stack implementation

  25. 25

    Having a little trouble with pointers

  26. 26

    Having trouble with C pointers

  27. 27

    pass pointers to pointers to function

  28. 28

    pass pointers to pointers to function

  29. 29

    Passing by reference arguments and pointers

HotTag

Archive