473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Memory help?

int main(int argc ,char **argv)
{
char p[100] = "welcome";
printf("%s", p) // prints welcome
printf("%d", &p); prints addr of p
printf("%c", *p); // prints 'w'
}

if p contains the value how come by referencing it gives the first
char in string
Aug 19 '08 #1
9 1156
raashid bhatt wrote:
int main(int argc ,char **argv)
{
char p[100] = "welcome";
printf("%s", p) // prints welcome
printf("%d", &p); prints addr of p
No. Invokes undefined behviour since the expected and actual type of 'p'
are different. To print pointer values use the %p conversion specifier:

printf("%p\n", (void *)&p);

The cast to void* is essential.
printf("%c", *p); // prints 'w'
}

if p contains the value how come by referencing it gives the first
char in string
Because in C strings are simply arrays of char and de-referencing the
pointer to the first element of the array 'p' gives you... you guessed
it, the character stored there.

So 'p' is an array object that contains the string "welcome", but when
you use the name of the array, in most contexts it is implicitly
converted to a pointer value to it's first element (which in this case
is a char) and the %c conversion specifier looks for a char* and prints
the character that it references.

You may want to look at the arrays and pointers section of the CLC FAQ
at <http://www.c-faq.com/>

Aug 19 '08 #2
In article <5a**********************************@b30g2000prf. googlegroups.com>,
raashid bhatt <ra**********@gmail.comwrote:
>int main(int argc ,char **argv)
{
char p[100] = "welcome";
printf("%s", p) // prints welcome
printf("%d", &p); prints addr of p
This is wrong: an address is not an integer and may well be a different
size. Use printf("%p", (void *)&p).
printf("%c", *p); // prints 'w'
}
>if p contains the value how come by referencing it gives the first
char in string
p is the name of the array and in most contexts that turns into the
address of the start of the array - a pointer to the array's first
element. When you printed it with %s, it took the pointer and printed
the characters starting at that address, up to the terminating null.
When you dereferenced it with *p, you got the single character
pointed to by p - the first character of the string.

You might ask what the difference is between p and &p. Both are the
address of the start of the array. But p has type pointer-to-char,
and &p has type pointer-to-array-of-100-chars.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 19 '08 #3
santosh <sa*********@gmail.comwrites:
raashid bhatt wrote:
<snip>
> char p[100] = "welcome";
printf("%s", p) // prints welcome
<snip>
No. Invokes undefined behviour since the expected and actual type of 'p'
are different. To print pointer values use the %p conversion specifier:

printf("%p\n", (void *)&p);
Personally, I would not take the address. There are few "general
rules" about C, but one I like to bear in mind is that code that does
X with an array often ends up in a function and has to do X but with a
pointer to the first element. I have got into the habit of having
another look at everything array-ish, and wondering if it will
survive, unchanged, a move into a context where the array is now just
a pointer.

--
Ben.
Aug 19 '08 #4
santosh wrote:
raashid bhatt wrote:
> printf("%c", *p); // prints 'w'
>if p contains the value how come by referencing it gives the first
char in string

Because in C strings are simply arrays of char and de-referencing the
pointer to the first element of the array 'p' gives you... you guessed
it, the character stored there.

So 'p' is an array object that contains the string "welcome", but when
you use the name of the array, in most contexts it is implicitly
converted to a pointer value to it's first element (which in this case
is a char) and the %c conversion specifier looks for a char* and prints
the character that it references.
ITYM the %c specifier looks for an argument of type int, the writes the
character (char)(unsigned char)argument_value.

--
Thad
Aug 20 '08 #5
You might ask what the difference is between p and &p. *Both are the
address of the start of the array. *But p has type pointer-to-char,
and &p has type pointer-to-array-of-100-chars.
you are wrong p and &p arent the same p points to the address where
the value is stored is "welcome" , and &p contains the address of
pointer itself
Aug 21 '08 #6
raashid bhatt <ra**********@gmail.comwrites:

context:
char p[100] = "welcome";
>You might ask what the difference is between p and &p. Â*Both are the
address of the start of the array. Â*But p has type pointer-to-char,
and &p has type pointer-to-array-of-100-chars.

you are wrong p and &p arent the same p points to the address where
the value is stored is "welcome" , and &p contains the address of
pointer itself
One statement is dubious because of an eminently excusable language
problem. If you'd said "points to the location where the value is
stored" there would be no problem with it, but in C "address" means
something very much like "pointer", so it is better to speak of a
"pointer to a location" rather than a "pointer to an address".

The second statement is simply wrong. How many people will have to
say this before you start to doubt your understanding?

By the way, don't clip attributions. The (entirely correct) text you
quote was written by Richard Tobin.

--
Ben.
Aug 21 '08 #7
raashid bhatt <ra**********@gmail.comwrote:
you are wrong
You do not know nearly enough to make that judgement.

Richard
Aug 21 '08 #8
raashid bhatt wrote:
>You might ask what the difference is between p and &p. Both are the
address of the start of the array. But p has type pointer-to-char,
and &p has type pointer-to-array-of-100-chars.

you are wrong p and &p arent the same p points to the address where
the value is stored is "welcome" , and &p contains the address of
pointer itself
You're being confused, I think, by the fact that in most circumstances
an expression that has an array type is implicitly converted to a
pointer to the first element of the array. For instance:

char *q = p;

Since 'p' gets converted into a 'char*' in the above context, it might
therefore seem as though the type of &p should be char**. This would
imply that the implementation has to create an unnamed pointer to that
array somewhere in user memory, and the give you a pointer to that
pointer. However, being the right operand of '&' is one of the three
contexts where an expression of array type does not get implicitly
converted to a pointer to the first element of the array (see
6.3.2.1p3). In this context, p refers to the entire array, and applying
the '&' operator to p gives you a pointer to the entire array, not a
pointer to a pointer. 6.5.3.2p3 says 'If the operand has type "_type_",
the result has type "pointer to _type".' Since the type of 'p' is
"char[100]", the type of &p is "char(*)[100]", not char**.
Aug 21 '08 #9
raashid bhatt wrote:
>You might ask what the difference is between p and &p. Both are the
address of the start of the array. But p has type pointer-to-char,
and &p has type pointer-to-array-of-100-chars.

you are wrong p and &p arent the same p points to the address where
the value is stored is "welcome" , and &p contains the address of
pointer itself
No.
(p) is an array.
In most contexts, (p) is converted to a pointer.
In this context:
(&p)
(p) is not converted to a pointer
and (&p) is the address of the array,
not the address of a pointer.


N869
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.
--
pete
Aug 21 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
7
by: Jon Trickey | last post by:
We migrated to 8.1 from 7.2 this weekend. Everything ran ok over the weekend, but we have a light user load then (about 200 users.) Today when we had close to 600 users connecting and running...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
1
by: Emmanuel Schnetzler | last post by:
Hi all, this might be a very basic question but I've been struggling with the problem for a while and can't find the solution. I am allocating a big chunk of memory that I want to deallocate...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
7
by: Clement | last post by:
hi, i have an asp.net site and is using SQL Server 2k. i realize the aspnet_wp.exe memory usage keep growing and i will receive an error for the pages that call the sql connection. others page...
7
by: trialproduct2004 | last post by:
Hi all I am having application in c# where i am loading one table of database into dataset. My table is of large size. so whenever i am loading that into dataset my memory size is getting...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.