C Programming - Typedef - Discussion

1. 

In the following code, the P2 is Integer Pointer or Integer?

typedef int *ptr;
ptr p1, p2;

[A]. Integer
[B]. Integer pointer
[C]. Error in declaration
[D]. None of above

Answer: Option B

Explanation:

No answer description available for this question.

Kumaran said: (Nov 14, 2010)  
Here we declared ptr as a pointer variable. Using that we can assign two pointers. First one hold the address of Ptr variable. And second one points the address of the first variable. Here the declaration is correct. And p1, p2 are not integers. So B is the answer.

Sunil said: (Dec 4, 2010)  
Then whether p1 is an integer. Please answer any one with explanation.

Lordy said: (Dec 18, 2010)  
@sunil

p1 is just a pointer. it is not declared as int so how can that be integer. think u will understand clearly.

Jigar said: (Jan 21, 2011)  
@lordy

but ptr is integer type pointer so i think indirectly p1 is also integer

Vengatraman said: (Jan 24, 2011)  
Becoz p1, p2 s assigned as ptr var & ptr is defined as integer. So I think as it is integer pointer.

Nishant said: (Mar 14, 2011)  
ptr is the pointer which has the address at which value is stored.And p2 is defined as ptr so p2 is the integer pointer which has the address at which has integer value...

Raju said: (Jul 6, 2011)  
Ptr is pointer to integer. Because p1 and p2 are declared as ptr and ptr is pointer to integer, p1 and p2 both will get the same functionality like integer pointer.

Teju said: (Aug 20, 2011)  
The typedef allows the new name become equivalent to the type you wanted.

Thus, since prt is pointer to integer; p1 and p2 are integer pointers.

Eg:

typedef int arr[7][3];

/* Now declare some objects */

arr a; // Here a is 7*3 array of int.

Prasad said: (Sep 23, 2011)  
*ptr is pointer varible, but when you assign p1 and p2 than p1 is hold the address of ptr but p2 is hold address of p1 ? please answer to me.

Parin said: (Dec 16, 2011)  
Understand the question like this,
typedef (int *) ptr;
ptr p1,p2;

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

Pankaj said: (Jan 1, 2012)  
We defined value at ptr(*ptr) as integer. So p1 and p2 not storing the adress their is they are storing value.

#include<stdio.h>
int main()
{
typedef int *ptr;
ptr p1, p2;
printf("%d %d",p1,p2);
}

o/p: 0 0

Martin said: (Jan 15, 2012)  
C is case sensitive and the question clearly asks what P2 is. There is no P2. There is a p2, but that is not the same. Therefore, the answer is D.

Divya said: (Jul 11, 2012)  
typedef int (*ptr) ;
ptr p1, p2;

What happens in this case ? what will be the type of ptr then ?

Sai said: (Aug 20, 2012)  
Excellent martin. Good observation.

Avnish said: (Aug 23, 2012)  
We defined value at ptr(*ptr) as integer. So p1 and p2 not storing the adress their is they are storing value.

#include<stdio.h>
int main()
{
typedef int *ptr;
ptr p1, p2;
printf("%d %d",p1,p2);
}

Lakshminaik said: (Sep 12, 2012)  
Any body help me how to create a struct function how may types.

Ramya said: (Sep 16, 2012)  
@Teju your explanation is very nice and thank you so much.

Aravind said: (Sep 28, 2012)  
What reason we can use typedef ?

Srees said: (Oct 2, 2012)  
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.

Dharshini said: (Mar 1, 2013)  
Give difference between #define and typedef?

Vineet said: (Mar 18, 2013)  
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: (Mar 29, 2013)  
@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: (Oct 28, 2013)  
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: (Feb 10, 2014)  
ptr is an integer pointer. Hence both the variables store the pointers.

Manju said: (Oct 14, 2014)  
ptr is an pointer variable, so the variable stored in pointer only.

Anis Gupta said: (Jul 23, 2015)  
Hi, typedef act as syntax replacement so simply.

ptr replace with int *,
So it is int *p1,p2;

Nikky Aaryan said: (Aug 4, 2015)  
*ptr is address of the variables and p1 & p2 are the integers.

int *ptr is nothing but the 'address of the variables are integers'.

Ashok said: (Dec 29, 2015)  
I can't understand the typedef operation?

Ganesh said: (Mar 8, 2016)  
What is type def?

Akshay Kalra said: (May 5, 2016)  
@Ganesh, @Ashok.

Typedef basically allows us to represent a data type with a new identifier.
Note---> typedef type identifier.

Therefore, ptr is not a pointer variable. It is just an identifier which represents integer pointer.

typedef int *ptr; // equivalent to typedef int * ptr
Hence, ptr p1,p2 // equivalent to int *p1, int *p2

Amit Saxena said: (May 24, 2016)  
@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.

Prasanth said: (Oct 28, 2017)  
Can we say like this?

*ptr is the name used instead of int and
ptr defines the variables as p1 and p2.
but how to compare the both *ptr and ptr.

Ruhi said: (Jan 3, 2020)  
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.

Jethalal said: (Mar 26, 2020)  
@Ruhi, Yes I also have the same doubt.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.