Re: pointers and strings
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sun, 03 Jul 2005 07:37:29 -0000
On Sun, 26 Jun 2005 09:57:41 -0000, "cooldudue" <vasanthkr@xxxxxxxxx>
wrote in comp.lang.c.moderated:
> Hi members
>
> i want to know that when i use
>
> char *p = "Hello";
> printf("\n Address of 'Hello' String is:%u", p);
When you use this, you are incorrect. It has undefined behavior, and
the C standard does not specify the result.
The only correct and defined way to print the value of a pointer to
any sort of object is to convert it to pointer to void and use the
"%p" conversion specifier:
printf("Address of 'Hello' String is: %p\n", (void *)p);
> is it similar
> char *p;
> p = malloc(5);
> *p = "Hello"
It is not similar at all, because the code above has a constraint
violation which means your compiler is required to output a
diagnostic. *p is a char, and you can't assign the address of a
string literal to a char.
And if you changed the code to make it legal (p = "Hello", not *'),
you would be creating a memory leak because you would be throwing away
the memory that you dynamically allocated.
> more doubts on the topics coming if i get reply
It seems possible that you are asking questions about topics beyond
your current level. Why exactly do you care about the representation
of the value of pointers? You can write standard, portable C on a
variety of platforms for an entire career without ever needing or
worrying about this.
What book on C are you using, and what is the underlying concept that
you are trying to understand?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- References:
- pointers and strings
- From: cooldudue
- pointers and strings
- Prev by Date: Re: strcat not working
- Next by Date: Re: Some stack questions
- Previous by thread: Re: pointers and strings
- Next by thread: Re: pointers and strings
- Index(es):
Relevant Pages
|