The 13th day of C language learning 1

Source: Internet
Author: User

#include <stdio.h>

int main (void)

{

int a[5]={1,2,3,4,5};

printf ("% #x,% #x \ n", a,&a[0]);

return 0;

}

Verify that a and &a[0] are the same value, verifying that a is the address of a[0] , a is a constant, its value cannot be changed.

#include <stdio.h>

void out (int *pa, int len)

{

pa[2]=10;

}

int main (void)

{

int a[5]={1,2,3,4,5};

Out (a,5);

printf ("%d\n", a[2]);

return 0;

}

Modify The value of the array a[2]

Pointer operations, pointers cannot be added, cannot be multiplied, and cannot be divided, if the two pointer variable points to a different storage unit in the same contiguous space, the two pointer variables can be subtracted, as follows:

#include <stdio.h>

int main (void)

{

int *p;

int *q;

int a[5];

p=&a[1];

q=&a[4];

printf ("P and Q points to a cell separated by %d units \ n", q-p);

return 0;

}

the value of 3 is obtained

#include <stdio.h>

int main (void)

{

Char ch= ' A ';

int i=99;

Double x=66.6;

Char *p=&ch;

int *q=&i;

Double *r=&x;

printf ("%d%d%d\n", sizeof (p), sizeof (q), sizeof (R));

return 0;

}

This is the amount of space that the pointer variable occupies, and the result is 8 bytes

Conclusion: A pointer variable, no matter how many bytes it points to, the pointer variable itself accounts for only eight bytes

Disadvantages of traditional arrays

1. The length of the array must be set in advance and can only be a long integer, not a variable

Example:int a[5]; OK int len=5;  int A[len]; Error

The second disadvantage is as follows :

#include <stdio.h>

int main (void)

{

int a[5]={1,2,3,4,5}; a byte of storage programmer cannot manually release it , it can only be released automatically by the system when the function is finished .

return 0;

}

3. Once the length of an array is defined, its length cannot be expanded or scaled up dynamically during function operation.

an array of 4.A function definitions that can be used by other functions while the a function is running, but after a function is finished,a The array in the function cannot be used by another function

malloc is the abbreviation for memory ( RAM ) Allocate ( allocation )

#include <stdio.h>

#include <malloc.h>

int main (void)

{

int i=5;

int *p= (int *) malloc (4);

*p=5;

Free (p);

printf ("%d\n", sizeof (*P));

return 0;

}

#include <malloc.h> call malloc int i=5; statically assigned the 4 bytes, int *p= (int *) malloc (4); malloc

4 malloc 12 bytes, p 8

4 p Span style= "FONT-FAMILY:CALIBRI;" >p *p=5; *p > represents a int variable, only

but *p The memory allocation method of this shaping variable is dynamic, Free (p); means to P The memory pointed to is released, P the memory itself is static and can only be released automatically by the function

#include <stdio.h>

#include <malloc.h>

int main (void)

{

int a[5];

int Len;

int *p;

printf (" Please enter the number of elements you want to store:");

scanf ("%d", &len);

p= (int *) malloc (4*len);

return 0;

}

#include <stdio.h>

#include <malloc.h>

int main (void)

{

int a[5];

int Len;

int *p;

printf (" Please enter the number of elements you want to store:");

scanf ("%d", &len);

p= (int *) malloc (4*len);

Free (p);

return 0;

}

int a[5]; because int accounted for 4 bytes, this array contains a total of - bytes, each four bytes is treated as a int variables to use

p= (int *) malloc (4*len); A one- dimensional array is constructed dynamically, and the length of the one-dimensional array is Len , the array name of the array is P , each element of the array is int type, class

seems to  int P[len]; Free (p); Releasing the dynamically allocated array

The 13th day of C language learning 1

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.