linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Steve Graegert" <graegerts@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Need for const in function argument list
Date: Sun, 26 Mar 2006 15:38:43 +0000	[thread overview]
Message-ID: <6a00c8d50603260738u2a0b35eau90fb22d93b16e289@mail.gmail.com> (raw)
In-Reply-To: <200603261908.36723.samjnaa@gmail.com>

On 3/26/06, Shriramana Sharma <samjnaa@gmail.com> 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


  reply	other threads:[~2006-03-26 15:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-26 10:44 Need for const in function argument list Shriramana Sharma
2006-03-26 10:57 ` Steve Graegert
2006-03-26 13:38   ` Shriramana Sharma
2006-03-26 15:38     ` Steve Graegert [this message]
2006-03-26 16:27     ` Glynn Clements
2006-03-27  8:25       ` Yorgos Pagles

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d50603260738u2a0b35eau90fb22d93b16e289@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).