Difference between const char* p, char * const ... - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Differencebetweenconstchar*p,char*constp,andconstchar*constpinC CServerSideProgrammingProgramming PointerInCprogramminglanguage,*prepresentsthevaluestoredinapointerandprepresentstheaddressofthevalue,isreferredasapointer.constchar*andcharconst*saysthatthepointercanpointtoaconstantcharandvalueofcharpointedbythispointercannotbechanged.Butwecanchangethevalueofpointerasitisnotconstantanditcanpointtoanotherconstantchar.char*constsaysthatthepointercanpointtoacharandvalueofcharpointedbythispointercanbechanged.Butwecannotchangethevalueofpointerasitisnowconstantanditcannotpointtoanotherchar.constchar*constsaysthatthepointercanpointtoaconstantcharandvalueofintpointedbythispointercannotbechanged.Andwecannotchangethevalueofpointeraswellitisnowconstantanditcannotpointtoanotherconstantchar.Thumbruleistonamingsyntaxfromrighttoleft.//constantpointertoconstantchar constchar*const //constantpointertochar char*const //pointertoconstantchar constchar*Example(C)Uncommentthecommentederrorneouscodesandseetheerror. LiveDemo#include intmain(){   //Example:charconst*   //Note:charconst*issameasconstchar*   constcharp='A';   //qisapointertoconstchar   charconst*q=&p;   //Invalidasssignment   //valueofpcannotbechanged   //error:assignmentofread-onlylocation'*q'   //*q='B';   constcharr='C';   //qcanpointtoanotherconstchar   q=&r;   printf("%c\n",*q);   //Example:char*const   charu='D';   char*constt=&u;   //Youcanchangethevalue   *t='E';   printf("%c",*t);   //Invalidasssignment   //tcannotbechanged   //error:assignmentofread-onlyvariable't'   //t=&r;   //Example:charconst*const   charconst*consts=&p;   //Invalidasssignment   //valueofscannotbechanged   //error:assignmentofread-onlylocation'*s'   //*s='D';   //Invalidasssignment   //scannotbechanged   //error:assignmentofread-onlyvariable's'   //s=&r;   return0; }OutputC E MaheshParahar Updatedon06-Jan-202006:30:36 RelatedQuestions&AnswersDifferencebetweenconstint*,constint*const,andintconst*inC Differencebetweenconstint*,constint*const,andintconst*inC/C++? Whatisthedifferencebetweenconstint*,constint*const,andintconst*? Howtoconvertanstd::stringtoconstchar*orchar*inC++? Howtoconvertastd::stringtoconstchar*orchar*inC++? Differencebetween#defineandconstinC JavaScriptConst Differencebetween++*p,*p++and*++pinc++ Differencebetween++*p,*p++and*++pinC Differencebetween#defineandconstinArduino DifferenceBetweenStaticandConstinJavaScript DifferencebetweenreadonlyandconstkeywordinC# ConstQualifierinC ConstcastinC++ Differencebetweenchars[]andchar*sinC PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?