آموزش زبان C (پروژه یازدهم: دسترسی به آرایه اشاره گرها از نوع int)

برنامه زیر یک برای دسترسی به یک آرایه اشاره گرها از نوع int را نشان می دهد:

#include <stdio.h>
/* 
    Global declaration. 
    Value of a const variable cannot be changed 
    throughout the execution of program
*/
const int MAX = 5;  

int main()
{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int var[]={10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers
    int i = 0;

    /*
        ptr is an array of int pointers i.e. 
        it stores the address of each array element
    */
    int *ptr[MAX];

    for(i = 0; i < MAX; i++)
    {
        /*
            Assign the address of each of the array 
            element to the ptr array
        */
        ptr[i] = &var[i];
    }

    for(i = 0; i < MAX; i++)
    {
        /*
            ptr[i] stores the address of the element var[i]. 
            Hence, *ptr[i] returns the value of the element 
            stored at location ptr[i]
        */
        printf("Value of var[%d] = %i\n\n", i, *ptr[i]);
    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

که با اجرا کردن اون خروجی به صورت زیر خواهد بود:

توضیحات کد:

printf("Value of var[%d] = %i\n\n", i, *ptr[i]);

در خط بالا عبارت ptr[i] آدرس المانهای آرایه var[i] رو ذخیره میکنه. و عبارت ptr[i]* مقدار المان ذخیره شده در موقعیت var[i] رو برمیگردونه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

قبلا حساب کاربری ایجاد کرده اید؟
گذرواژه خود را فراموش کرده اید؟
Loading...