Re: [RFClue] Constant declaration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> From: Felix Oxley <lkml@xxxxxxxxx>
> 
> In certain places I have seen the following code:
> (e.g. scripts/pnmtologo.c or arch/ppc/platforms/chrp_setup.c)
> 
> static const char *foo = "ABC";
> 
> int main(int argc, char *argv[])
> {
>         foo = "some other value";
> }
> 
> 
> How can this be allowed? foo has been declared constant and yet is being 
> assigned a new value ?

The "const char *" means it's a pointer to constant characters.  The characters are constant, but not the pointer.  The assignment, foo = "...", changes the pointer, but not the characters, so it's valid.

> Also, I have seen a lot of: const char *foo = "";
> There would be no point in declaring a constant empty string so it must be 
> given a new value elsewhere in the code.

Again, the characters are empty, but not the pointer, so the pointer may be assigned to a different character string.

HTH
Mark


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux