C Language---Advanced pointer 2 (struct pointer, array as function parameter)

Source: Internet
Author: User

First, the structural body pointer

1. What is a struct pointer? Pointer to struct-body variable

struct: typedef struct stu{

Char name[20];

char sex;

int age;

}student;

Student stu1 = {"Zhangsan", ' m ', 23};

Student *p = &stu1;

p is a struct pointer that holds the address of the first member of a struct variable.

Student stu2 ={"Lisi", ' m ', 26};

*p = STU2;

(*p). Sex = ' f '; modifying struct variables (usually with p->sex = ' f ')

2. Structure array and pointer relationship

Student Stus[5] = {

{"Zhangsan", ' m ', 23},

{"Lisi", ' F ', 24},

{"Wangwu", ' m ', 28},

{"Zhangda", ' m ', 17},

{"Qianer", ' F ', 22}

}

Student *p1 = &stus[3];p The first address to stu[0], the length of the address is Student type

Student *p2 = stus; The array name represents the first address of the array, which is the address of the first element (Stu[0]) of the array.

Change the gender of Zhaoda to female, p1->sex = ' f ';

3. struct student{

Char *name;

}

member defined as Char name[20], difference?

Char name[20] occupies 20 bytes and is actually in memory.

Char *name occupies 8 bytes pointing to the name of the constant area, which does not exist itself, is an address and is a pointer.

4. When an array is used as a function parameter, the first address of the array is actually passed to the function.

Statement: Printmaxvalueofarray (int a[100]);

System default: Printmaxvalueofarray (int *a);

That is, regardless of the definition of a[200] or a[100], the system defaults to the first address of array A is a.

Note: It is either defined as int a[], or it is defined as an int *a, and two must be followed by an int count;

Main function:Printmaxvalueofarray (A,sizeof (a)/sizeof (a[0]));

5. Pointers can be used as arrays

void PrintArray (int *arr,int count);

void PrintArray (int *arr,int count) {

for (int i = o; i < count; i++) {

Print ("%d", arr[i]);

}

}

Pointers can be used as arrays, and arrays can also be used as pointers

Note: The array acts as a function parameter, and the formal parameter represents only the first element address of the array, the number of incoming

6. Strings and pointers

A string is actually a character array.

file:///Users/lanou3g/Library/Containers/com.tencent.qq/Data/Library/Application%20Support/QQ/Users/787053796/ Qq/temp.db/c744e1c4-8b16-4a06-ab53-1a8e69f411d6.png

Pointer + + moves one type so long distance at a time

C Language---Advanced pointer 2 (struct pointer, array as function parameter)

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.