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.

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.

AKSHAY KALRA said:   9 years ago
@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

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)

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.

Kumaran said:   1 decade ago
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.

Teju said:   1 decade ago
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.

Pankaj said:   1 decade ago
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

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.

Avnish said:   1 decade ago
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);
}

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)


Post your comments here:

Your comments will be displayed after verification.