C basics: comparing "*char" and "char[x]" for string creation -


A very basic question Why does the following two orders result in the creation of two different sized stars?

As you can see below, method 1 creates a string with size 8 bytes and method 2 creates a string with size 5 bytes.

Why the method 1 is causing the confusion as to the size of the 8 bytes ...

(I've already seen these posts: and as long as I Reading-understanding-does not fail, it does not really know why the method makes a string of 1 size 8 bytes ... According to the reactions, it appears that method 1 should be made an indicator of size 4 bytes. )

Method 1:

  char * string = "ABCD";   

Method 2:

  Four string 2 [5] = "ABCD";   

For example, when I run the following program, I get the output shown below.

  #include & lt; Stdio.h & gt; Int main (int argc, char * argv []) {char * string = "ABCD"; Printf ("String size:% ld \ n", sizeof (string)); Printf ("the size of each element of the string"; \ "ARCD \": \ n "(" string:% s \ n \ n ", string); four string 2 [5] =" ABCD \ 0 "; printf "String 2"] "[4] String 2 [5] = \" ABCD \ ": \ n"); Printf ("String size:% ld \ n", size (string 2)); printf (" The size of each element:% ld \ n ", the size (string 2 [0]); printf (" string:% s \ n \ n ", string 2); return 0;}   

Output of the above program:

Enter image details here < / div>

That's because in this line, char * string = "ABCD"; Pointer is declaring, which is a size of 8 bytes where this line as four string 2 [5] = "ABCD"; You can see an array of 5 characters and a four < / code> 1 byte is long and 1 * 5 == 5 bytes then string2 will be 5 bytes

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -