C Language Learning 004: array and pointer, language learning 004

Source: Internet
Author: User

C Language Learning 004: array and pointer, language learning 004

In C, strings are actually character arrays. In memory, strings are stored in the form of "Shatner ".

Because the C language does not know how long the array is, "\ 0" is used to indicate the end position of the string. the sizeof operator can be used to obtain the number of bytes occupied by the string in memory.

 

The length of the same string in the following code is different. Do you know why?

#include <stdio.h>void SayHello(char msg[]){    printf("msg occupies: %i\n",sizeof(msg));}int main(){    printf("Shatner occupies: %i\n",sizeof("Shatner"));    char msg[]="Hello C";    printf("msg occupies: %i\n",sizeof(msg));    SayHello(msg);    char* msgp=msg;    printf("msgp occupies: %i\n",sizeof(msgp));    return 0;}

1 void SayHello (char msg []) {// if the function parameter is declared as an array, it will be processed as a pointer, so the msg here is actually the pointer variable 2 printf ("msg occupies: % I \ n", sizeof (msg )); // Therefore, sizeof obtains the pointer length 3} 4 5 int main () {6 printf ("Shatner occupies: % I \ n", sizeof ("Shatner ")); 7 char msg [] = "Hello C"; // The computer allocates space for each character of the string and the ending character "\ 0" on the stack, and associate the first character address with msg, 8 printf ("msg occupies: % I \ n", sizeof (msg); 9 SayHello (msg ); // array can use 10 char * msgp = msg when the pointer is used; 11 printf ("msgp occupies: % I \ n", sizeof (msgp )); // here, of course, the pointer length is also obtained 12 return 0; 13}The Code explains the differences between arrays and pointers.

In the code above, we can also see that the sizeof (array) and sizeof (pointer) results are different, sizeof (array) is the size of the array and sizeof (pointer) what we get is the pointer size in the operating system (4 in 32 bits and 8 in 64 bits );

Use the & operator to get the address of the array. The array variable also obtains the address of the array itself. The pointer variable obtains the address of the array, the "&" operator pointer variable obtains the address stored in the pointer variable value (the address of the array address)

The computer does not allocate memory to array variables, so it cannot point to other places. However, the computer allocates space for pointer variables.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.