# typedef function in c

Page 1 of 1

## 5 Replies - 395 Views - Last Post: 17 July 2012 - 11:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285876&amp;s=64dea67079284e343c2a9eb7f6daa576&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 prasad manne

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 31-May 12

# typedef function in c

Posted 15 July 2012 - 10:48 AM

```typedef char *chrptr;
const chrptr p;

```

in the above two lines, char is defined by *chrptr and that is declared as const
now i have 4 options
A. P is a constant
B. P is a character constant
C. P is character type
D. None of above

i am in puzzle whether p is a character constant or a constant
can you please clarify me with some explanation??
Is This A Good Question/Topic? 0

## Replies To: typedef function in c

### #2 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: typedef function in c

Posted 15 July 2012 - 11:35 AM

"Unroll" the typedef an you come up with this.

```const char *p;
```

It's a pointer of type char to constant memory.

This post has been edited by Aphex19: 15 July 2012 - 11:35 AM

### #3 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: typedef function in c

Posted 15 July 2012 - 01:01 PM

I do not understand the infatuation with hiding the asterisk.

Don't do this in real code please.

### #4 Skydiver

• Code herder

Reputation: 4261
• Posts: 13,640
• Joined: 05-May 12

## Re: typedef function in c

Posted 15 July 2012 - 08:19 PM

KYA, on 15 July 2012 - 01:01 PM, said:

I do not understand the infatuation with hiding the asterisk.

Don't do this in real code please.

<sarcasm>
Many people have damaged their wrists/hands/fingers using emacs and the overused Ctrl key. To decrease chances of further damage, they don't want to hit the Shift key anymore than they have to. Notice that these same people also seem to avoid underscores.
</sarcasm>

Seriously, though, I think that you posed a great question. I think that it's a style that certain schools/teachers espouse. I don't know the reasons behind the choice of the style.

My personal theory is that they hide the asterisks to avoid the religious war during code reviews about trying to decide which is "correct":
```char* pchAttachdToType;
char *pchAttachedToVariable;
char * pchFreeFloating;
chrptr pchAvoidTheIssueCompletely;

```

This post has been edited by Skydiver: 15 July 2012 - 08:20 PM

### #5 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: typedef function in c

Posted 17 July 2012 - 08:35 PM

As we ALL know, the first one is the correct one.

### #6 Salem_c

• void main'ers are DOOMED

Reputation: 1933
• Posts: 3,810
• Joined: 30-May 10

## Re: typedef function in c

Posted 17 July 2012 - 11:04 PM

> As we ALL know, the first one is the correct one.
Unless you know the rules, what type is c in this declaration?
```//c99 code - in case you're wondering about %zd
#include<stdio.h>
int main()
{
char* p, c;
printf("%zd %zd\n", sizeof(p), sizeof(c) );
return 0;
}

```

Placing the * to the type sends the wrong stylistic message to the reader IMO.
Writing char *p,c; should suggest that the * is applied to p only, and that c is just a char (which it is).

Better still would be to not mix such declarations to begin with, but if you're going to do it, make sure that it reads properly.