資料結構複習之C語言指標與結構體

來源:互聯網
上載者:User

標籤:div   表示   複雜   屬性   ...   class   變數   個數   struct   

資料結構指標複習:
#include <stdio.h>void main(){    int a[5] = {8, 2, 3, 4, 5};        // a[3] == *(3+a)    printf("%d\n", *(3+a)); // a[3] 4    printf("*a其實就是a[0]: %d\n", *a); // 8    // 地址是連續的    printf("%p\n", a+1);    printf("%p\n", a+2);    printf("%p\n", a+3);}

一維數組名是個指標常量,它存放的是一維數組第一個元素的地址,它的值不能被改變,
一維數組名指向的是數組的第一個元素的地址。(擷取)修改一個數組,就是要把數組的第一個元素的地址和數組的長度傳遞進去就行了。

#include <stdio.h>void Show_Array(int *p, int len){    p[0] = -1;    int i;        for(i = 0; i < len; i++)    {        p[i] = p[i] + 1;        printf("%d\n", p[i]);    }}int main(void){    int a[5] = {1, 2, 3, 4, 5};    Show_Array(a, 5); // a等價於&a[0], &a[0]本身就是int *類型    return 0;}
通過函數修改實參的值:
#include <stdio.h>void f(int *p){    *p = 99;}int main(void){    int i = 10;        f(&i);    printf("%d\n", i);        return 0;}

結構體使用概述:結構體只有屬性,沒有方法。結構體是類的一個過渡。為什麼會出現結構體?    為了表示一些複雜的資料,而普通的基本類型無法滿足要求。什麼叫結構體?    結構體是使用者根據實際需要自己定義的複合資料型別。
#include <stdio.h>#include <string.h>struct Student    // 定義了一個新的資料類型{    int sid;    char name[200];    int sage;    }; // 分號不能省int main(void){    // 定義一個資料類型是struct Student的變數st    struct Student st = {1000, "zhangsan", 20};    printf("%d %s %d\n", st.sid, st.name, st.sage);        st.sid = 99;    strcpy(st.name, "lisi");    st.sage = 22;    printf("%d %s %d\n", st.sid, st.name, st.sage);        return 0;}

 

 

 未完待續......

資料結構複習之C語言指標與結構體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.