Re: Need for const in function argument list

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

 



On 3/26/06, Shriramana Sharma <samjnaa@xxxxxxxxx> wrote:
> Sunday, 26 March 2006 16:27 samaye, Steve Graegert alekhiit:
> 
> > Because the format (the string to display) is provided as a constant
> > expression and it is not being modified:
> > 	printf("%s\n", "abc");
> > 	printf("%s\n", mystring);
> > In both cases the format argument is a constant string.
> 
> Why, the following works as well:
> 
> #include "stdio.h"
> void main(void)
> {
> 	char s[10] = "\n%s\n\n";
> 	printf(s, "hello");
> }
> 
> Here s is not a const char *. It is a variable char *. That actually
> compiled  and executed. 

Yes, it compiles and why should it not do so?  It's a __string constant__, since it cannot be changed in any way.  It's effectively the same as the statement

	char *s = "\n%s\n\n"

The situation is different with malloc(2).  It allows for dynamic allocation of memory, thus turning string constants into dynamic data structures:

	char * s;
	s = (char *) malloc(100);

Every string inside double quotes (" ") is a character constant.
 
	\Steve

-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux