Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an integer pointer to a function?

Here i am executing a simple code in C. It is compiling fine but traps during run time.

#include <stdio.h>

#include <conio.h>

void sum(int x,int y,int *z)
{
    *z=x+y;
}

void main()
{
    int a=10,b=20,*c;

    sum(a,b,c);
    printf("sum is %d\n",*c);
}

Can someone point out what is the issue? Also, how does one pass a pointer to a function?

like image 591
Prashanth Cm Avatar asked Jul 30 '15 02:07

Prashanth Cm


People also ask

How do you pass an int pointer to a function?

Passing Pointers to Functions in C++ C++ allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type.

How do you pass a pointer to a function example?

Example 2: Passing Pointers to Functions Here, the value stored at p , *p , is 10 initially. We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the addOne() function. Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .

Can we have a pointer to a function?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

Can we pass pointer as an argument to function?

Pointer argument to functionWe can also pass the pointer variable to function. In other words, we can pass the address of a variable to the function instead of variable value.


2 Answers

Your mistake is that you have passed an uninitialized integer pointer to a function and then used the pointer. What you probably intended to do was to automatically allocate an integer on the stack and then pass the address of said integer to the function.

#include <stdio.h>
#include <conio.h>

void sum(int x,int y,int *z)
{
    *z=x+y;
}

void main()
{

    int a=10,b=20,c;  // Automatically allocate an integer on the stack

    sum(a,b,&c); // Third argument is the address of the integer
    printf("sum is %d\n",c);
}

The key thing is to remember that when you do int *c you are allocating a pointer, when you do int c you are allocating an integer. If you wish to modify a variable passed to a function, the typical pattern in C is to pass the address of said variable but you first need to allocate the proper type, in this case an int and not an int *. You can then use the address of operator & to obtain the address of the relevant data which you then pass as the function argument.

like image 158
missimer Avatar answered Oct 25 '22 23:10

missimer


Problem in your above program is unitialized pointer c. So you can allocate memory to c using malloc -

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

 void sum(int x,int y,int *z)
  {
        *z=x+y;
  }

 int main()   // declare main as int.
 {

     int a=10,b=20,*c;
     c=malloc(sizeof(int));    //allocating memory to pointer c 
     sum(a,b,c);
     printf("sum is %d\n",*c);
     free(c);                  //  freeing allocated memory
     retrirn 0;
 }
like image 41
ameyCU Avatar answered Oct 25 '22 22:10

ameyCU