Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is the difference between char a[] = “string”; and char *p = “string”; ?
What is the difference between char a[] = “string”; and char *p = “string”; ?
✍: Guest
Answer1
a[] = “string”;
char *p = “string”;
The difference is this:
p is pointing to a constant string, you can never safely say
p[3]=’x';
however you can always say a[3]=’x';
char a[]=”string”; - character array initialization.
char *p=”string” ; - non-const pointer to a const-string.( this is permitted only in the case of char pointer in C++ to preserve backward compatibility with C.)
Answer2
a[] = “string”;
char *p = “string”;
a[] will have 7 bytes. However, p is only 4 bytes. P is pointing to an adress is either BSS or the data section (depending on which compiler — GNU for the former and CC for the latter).
Answer3
char a[] = “string”;
char *p = “string”;
for char a[]…….using the array notation 7 bytes of storage in the static memory block are taken up, one for each character and one for the terminating nul character.
But, in the pointer notation
char *p………….the same 7 bytes required, plus N bytes to store the pointer variable “p” (where N
depends on the system but is usually a minimum of 2 bytes and can be 4 or more)……
2012-03-08, 2947👍, 0💬
Popular Posts:
Jack developed a program by using a Map container to hold key/value pairs. He wanted to make a chang...
How do we host a WCF service in IIS? Note: - The best to know how to host a WCF in IIS is by doing a...
What is CodeDom? “CodeDom” is an object model which represents actually a source code. It is designe...
What is the purpose of finalization? The purpose of finalization is to give an unreachable object th...
How To Create an Add-to-Bloglines Button on Your Website? - RSS FAQs - Adding Your Feeds to RSS News...