what would be the output of the following program?
main()
{
int k = 123;
char *ptr;
ptr = &k;
printf("%d",*ptr);
}
Answers were Sorted based on User's Feedback
Answer / vadivelt
Output would be 123.
Since, the character pointer can hold the values
0 - 255(if it is unsigned) or -128 to 127 (if it is signed), we
will get value of k as result.
But if the k value is k > 255 and the pointer is unsigned,
or if the k value is k > -129 and k < 128 and the pointer
is signed then only lower 1 byte of k would be the result.
Remaining data will be lost.
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / vadivelt
Hi Srsabariselvan,
If you are not very clear on the answer, please avoid to post
it. Because your answer seems to be misguiding the persons
who are very new to this question(probly pointers).
Who said that a pointer has to hold the address of same
datatype????.... a pointer of any datatype can hold the
address of any other data types(only it is enough to have
proper typecasting).
There will not be a compilation error. But most of the time
loss of data may be there(ie., when a bigger size of
datatype is typecasted to smaller eg: int* is typecasted to
char*). Please read the answer #1 for clear understanding.
Still if you are not clear on the concept, Copy the code
and execute it in ur compiler and analys the output.
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / srsabariselvan
The program results in compilation error.
NOTE: a pointer can stores the address of same data type.
it can't store the address of another data type.
i.e.,
character pointer can stores the address of character data.
it can't store the address of integer data.
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / ankush
i also have a compilation error.....
""Cannot convert 'int_ss*' to 'char*' in function main()""
why its so....???
i want some answer.....
is my compiler wrong....
Is This Answer Correct ? | 0 Yes | 0 No |
pick out the odd one out of the following a.malloc() b.calloc() c.free() d.realloc()
write a program to concatenation the string using switch case?
Where are c variables stored in memory?
Find the O/p of the following struct node { char *name; int num; }; int main() { struct node s1={"Harry",1331}; struct node s2=s1; if(s1==s2) printf("Same"); else printf("Diff"); }
What is zero based addressing?
Can one function call another?
What is the benefit of using an enum rather than a #define constant?
why use "return" statement a) on executing the return statement it immediately transfers the control back to the calling program b) it returns the value present in the parentheses return, to the calling program c) a & b d) none of the above
Can we assign string to char pointer?
how to find a 5th bit is set in c program
/*program to calculate hra,da in salary if salary less than 10000 then hra15%,da13% otherwise hra20%,da18%/*
What is maximum size of array in c?