开发者

i am getting error please help me to resolve this error..i have written code below [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide a开发者_高级运维udience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.
#include<stdio.h>
void main()
{
    int *j;
    void fun(int);
    fun(&j);
}
void fun(int *k)
{
    int a=10;
    k=&a;
    printf("%d\n%d\n%d\n",k,&a,*k);
}


You need to have a "forward declaration" of fun before main

void fun(int* k);
int main()
{
...


Maybe this will help you understand what's going on...

Having the declaration within Main() is OK, your problem is that it's incorrect. It doesn't follow your usage of it. When you call fun() you're passing an int** to it, your declaration says that fun() takes an int and not an int**.

You've declared a int* j, then when you pass it into fun you're passing (the address of the pointer, which ends up being a int**). This is not what the declaration says.

Tip: use %p instead of %d when printing pointer values.

And by the time I finished typing this every answer already had pretty much the same stuff... :-)


I am wondering, what do you wanna do with that code? I thought of this:

#include<stdio.h>
void fun(int*);
int main()
{
    int j;
    fun(&j);
    return 0;
}
void fun(int *k)
{
    int a=10;
    k=&a;
}

is this what you want? Because there are some weird stuffs in your code:

  1. You declare void fun(int) in your main()
  2. the function fun takes a pointer, not an int
  3. you try to print an address with %d (usually with %p?)
  4. you pass the address of * j to fun(int *)


You need to declare the prototype for fun() before main() and not in main(). Also the parameter list for the prototype and the function definition need to be the same. When you call fun() you need to pass a parameter of the correct type. You need to pass an int *, you're passing an int **.

But even if you fix those obvious errors your program probably won't do what you expect. You really need to learn about pointers and how to use them.


Adding on the the issues pointed out in other answers,

int *j;

You are declaring j which can point to int type variables.

fun(&j);

You are passing the address of the pointer variable(int**), to a function which expects an int*

Suggestions: Change to

void fun(int **k){ //Need to change declaration. Plus, the rest of the fun(..) body wouldnt make much sense though.

or call like

fun(j);


I'll list a few errors here.

    void fun(int);

This declaration does not match the actual function definition, which takes an int *.

    fun(&j);

This does not match either the declaration above or the actual function definition. It passes the address of a pointer, or int **.

    printf("%d\n%d\n%d\n",k,&a,*k);

Don't use %d to print addresses. Use %p instead.


As well as all the things that the compiler will warn you about - function declaration not matching definition, passing wrong type of argument to fun, passing wrong arguments to printf etc etc what you are attempting to do is silly anyway as int a is on the stack so address of int a that is stored in j will not be valid once the fun returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜