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 5 of 5.

Shivangi said:   1 decade ago
If the function would have declared static than it would be correct to pass static parameters inside a function.

Sushovan said:   10 years ago
Parameters will not be static. Its not allowed.

Sujan said:   9 years ago
What would be the answer if it wasn't static?

Praju said:   9 years ago
Can anyone please explain question with the correct program?

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)

Avinash said:   9 years ago
What kind of function is this?

int *check();

I mean what does the * sign denote here?

Amit said:   8 years ago
Static is not allowed.

Omar said:   5 years ago
@Avinash.

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

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)

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)


Post your comments here:

Your comments will be displayed after verification.