C Programming - Typedef - Discussion

Discussion Forum : Typedef - General Questions (Q.No. 1)
1.
In the following code, the P2 is Integer Pointer or Integer?
typedef int *ptr;
ptr p1, p2;
Integer
Integer pointer
Error in declaration
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 1 of 4.

Amit Saxena said:   9 years ago
@All.
I think we should try the given codes in c compiler provided.

@Pankaj.
I tried your code and definitely p1 and p2 are working as an integer, not as integer pointers.
(2)

Akash said:   5 years ago
@Ruhi.

It will change the name of int* as ptr.
(1)

Ruhi said:   6 years ago
How is it possible?

Whenever we use typedef, that means we are renaming the previously defined keyword or variable.

If we have declared,

Typedef int *ptr;

That means simply we have renamed, int as *ptr. Then how did it become a pointer?

Please answer me with a clear explanation.
(1)

Parin said:   1 decade ago
Understand the question like this,
typedef (int *) ptr;
ptr p1,p2;

Now it is very clear that p1 and p2 are integer pointers..
(1)

Dharshini said:   1 decade ago
Give difference between #define and typedef?

Srees said:   1 decade ago
Lets study each line. For expl typedef int A, if we given means A=int (i. E, we can use A instead of int datatype) so here we have typedef int *ptr (i. E, we can use ptr as int) so ptr p1, p2 means integer pointers.

Vineet said:   1 decade ago
typedef int *ptr;

Tt means ptr can be used in place of (int *), it is a alais name.

Where we write ptr it means we can substitute it with int *;

Pooja said:   1 decade ago
@Dhanashri.

1). #define: It is used to define micro which are small & faster, it is preprocessor detectives run automatically by compiler by that our source code get changed.

2). typedef: It is keyword used to make variable of particular data type but indirectly.

EX:
typedef char a[]={"dhanashri"};
a b[]={ "pooja"};
where b is char type even though it is indirectly.

Abhayraj said:   1 decade ago
typedef int *ptr is a ptr declaration & p1,p2 behaves as objects of ptr.

Hence, similar to *ptr & ptr , *p1,*p2 print values at their address & p1, p2 print memory location address. I have compiled on linux gcc.

Please, comment for my answer, for any correction.

Lakshmi prasanna said:   1 decade ago
ptr is an integer pointer. Hence both the variables store the pointers.


Post your comments here:

Your comments will be displayed after verification.