5 Replies - 336 Views - Last Post: 17 July 2012 - 11:04 PM Rate Topic: -----

#1 prasad manne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • 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.
Was This Post Helpful? 4
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: typedef function in c

Posted 15 July 2012 - 08:19 PM

View PostKYA, 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

Was This Post Helpful? 2
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1743
  • View blog
  • Posts: 3,360
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1