0
#include<stdio.h>
int sum(int,int);
int main()
{
    int a=5;
    int b=6;
    int res;
    res=sum(a,b);
    printf("\n%d\n%d\n",a,b);
    printf("%d",res);

}
int sum(int x,int y)
{
    x=x+1;
    y=y+3;
    return y;
}

This is a sample program where my a and b values didint change and the result of modified y (ie)9 is returned back to main program...but my doubt is if iam doing the same program using pointers i am getting "CONVERITNG INT** INT* is NOT POSSIBLE???CAN U explain ....thank you..........

#include<stdio.h>
int* sum(int,int*);
int main()
{
    int a=5;
    int *b;
    *b=6;
    int *res;
    res=sum(a,&b);
    printf("\n%d\n%d\n",a,*b);
    printf("%d",*res);

}
int* sum(int x,int *y)
{
    x=x+1;
    *y=*y+3;
    return (&y);
}

iam very weak in pointers can you suggest me how to improve my skills in pointers........thanks in advance :)

2
Contributors
1
Reply
21
Views
2 Years
Discussion Span
Last Post by skaa
1

This line:

int *b;

just tells the machine that address b will point to integer, and what memory adress contains b we do not know. Before the line

*b=6;

you should allocate place in the memory to put number 6 there:

b=new int[1];

. So, it should be like:

int *b;//Declare address for integer
b=new int[1];//Allocate memory for integer and assign its address to b
*b=6;//Put 6 into this part of memory
delete b;//Do not forget this line :), it cleans memory
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.