C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 8)
8.
What will be the output of the program ?
#include<stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d\n", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}
10
20
Error: Non portable pointer conversion
Error: cannot use static for function parameters
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
50 comments Page 1 of 5.

Harikrishnan V said:   9 years ago
I would like to add one more thing.
#include<stdio.h>
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}
int *check(int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}


This removes the 'static' keyword, then also will not give the correct output as the pointer p is local to the function and on the return of control from the function it gets destroyed - returning a garbage value.
(3)

Dharmesh said:   3 years ago
Static variables compiler stores in static/global (data section) as per storage class.

Here we are having pointers present in the code section pointing to static variables which are present in other storage sections giving error.

Also, we cannot use static variables as function parameters in C.
(2)

Omar said:   5 years ago
@Avinash.

* means that's this function return pointer to integer value.
(2)

Yash said:   1 year ago
#include<stdio.h>
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", *c);
return 0;
}
int *check( int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}

Here we get the output is 20.
(1)

Ashi said:   1 decade ago
Can anyone tell me what is the reason why we can not pass static variables in a function?

Karthik said:   1 decade ago
Can any one explain that, what happens when the parameters are non-static?

Anonymous said:   1 decade ago
Static variables can not be declared inside method. These are always declared inside the main method.

Damodharam said:   1 decade ago
@ M@C:

The static variable is initialized to "1" as global variable and in local block variable is initialized to "10". Since both are same variables when the local block finishes and comes to the function call it will take value of global variable and it is printed as output.

Rasu said:   1 decade ago
I am using visual c++, in that I can able to pass static variable as the arguments, it is showing only warning.

Sourav pal said:   1 decade ago
Because static variable can not be change, It is initialized at one time. So answer is 1.


Post your comments here:

Your comments will be displayed after verification.