C programming problem

GT9

Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    මචංලා C වල වේරියබල්ස් කීපයකට එක සමාන memory address තියෙන්න පුලුවන්ද?

    මේ උදාහරණය බලන්න.


    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 10;
        int num2 = 110;
        
        printf("%d live in %p\n",num);
        printf("%d live in %p\n",num2);
    return 0;
    }

    මේ විදියට තමයි console එකේ output එක ‍එන්නේ.

    Code:
    10 live in 0022FF94
    110 live in 0022FF94
     

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    48,460
    98,391
    113
    මචංලා C වල වේරියබල්ස් කීපයකට එක සමාන memory address තියෙන්න පුලුවන්ද?

    මේ උදාහරණය බලන්න.


    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 10;
        int num2 = 110;
        
        printf("%d live in %p\n",num);
        printf("%d live in %p\n",num2);
    return 0;
    }
    මේ විදියට තමයි console එකේ output එක ‍එන්නේ.

    Code:
    10 live in 0022FF94
    110 live in 0022FF94
    http://stackoverflow.com/questions/8338490/memory-address-of-c-variables
     
    • Like
    Reactions: GT9

    Arkham Knight

    Member
    Mar 14, 2014
    1,627
    89
    0
    මෙහෙමයි ඔතන තියෙනවා නේ printf() function එක ඒකෙන් කරන්නේ value එක stream එකකට ගන්න එකයි (output stream) ඊට පස්සේ ඒක output එකට flush කරනවා.
    ඕනෙම stream (දිය පහරක් වගේ) එකක තියෙනවා අග්‍ර 2ක්. ඔතන එක අග්‍රයක් සෙට් වෙන්නේ application එකට අනෙක් අග්‍රය සෙට් වෙන්නේ output device එහෙකට.

    default output එක තමයි console එක (ඒක මාරු කරන්නත් පුලුවන්) එතනදී value එක යන්නේ එකම address එකක් හරහා මොකද function එක same නිසා address එක ගන්න නම් pointer එකක් මගින් වෙනමම ඒක ගන්න variable එකකට ඊට පස්සේ ඒක print කරන්න දෙන්න නැතුව ඔතනදි address එක ගන්න එපා.

    C++ වලදි ගන්නේ ඕක මෙහෙමයි මේක C වලටත් same cout එක නැතුව දාන්න.

    int i = 10;
    int *p = &i;
    cout << p << endl;
     
    • Like
    Reactions: GT9

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    මෙහෙමයි ඔතන තියෙනවා නේ printf() function එක ඒකෙන් කරන්නේ value එක stream එකකට ගන්න එකයි (output stream) ඊට පස්සේ ඒක output එකට flush කරනවා.
    ඕනෙම stream (දිය පහරක් වගේ) එකක තියෙනවා අග්‍ර 2ක්. ඔතන එක අග්‍රයක් සෙට් වෙන්නේ application එකට අනෙක් අග්‍රය සෙට් වෙන්නේ output device එහෙකට.

    default output එක තමයි console එක (ඒක මාරු කරන්නත් පුලුවන්) එතනදී value එක යන්නේ එකම address එකක් හරහා මොකද function එක same නිසා address එක ගන්න නම් pointer එකක් මගින් වෙනමම ඒක ගන්න variable එකකට ඊට පස්සේ ඒක print කරන්න දෙන්න නැතුව ඔතනදි address එක ගන්න එපා.

    C++ වලදි ගන්නේ ඕක මෙහෙමයි මේක C වලටත් same cout එක නැතුව දාන්න.

    තෑන්ක්ස් අයිය. අයිය කියල දීපු විදියට මම ට්‍රයි කරා පහත තියෙන්නේ ඒ කෝඩ් එක එතනදී අවස්තා දෙකේදී එකිනෙකට වෙනස් මෙමරි ඇඩ්රස් 2ක් ලැබෙනවා. හැබැයි ඒ වෙනසත් පොඩි වෙනසක් :P මම ඩෙසිමල් value එකත් ඒ එක්කම බැලුව ඒ දෙක එක සමානයිනේ? :dull::dull::dull:

    මම දන්න විදියට ඔතන ඇවිත් තියෙන ඩෙසිමල් වැලුයි එක මෙමරි ඇඩ්රස් එකේ හෙක්ස ඩෙසිමල් ආකාරය. (හරියටම නොදනී :no::no::no:)


    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 10;
        int num2 = 110;
    
        int *num_p = &num;
        int *num_p2 = &num2;
    
        printf("%p - %d\n",num_p);
        printf("%p - %d\n",num_p2);
    return 0;
    }

    console:

    Code:
    0022FF14 - 2293652
    0022FF10 - 2293652

    මේක අයිට වෙලා තියෙනවනම් ටිකක් පැහැදිලි කරල දෙන්න පුලුවන්ද? මම pointers ගැන ටිකක් කියෙව්ව, වීඩියෝස් බැලුව. ඒත් මේ එකිනෙකට වෙනස් variables 2ක ඇඩ්රස් එක සමාන වෙන්න පුලුවන් කතාවක් මට තාම ඉගෙන ගන්න බැරි උනා. union ද මොකක්ද කතාවක් දැක්කා සර්ච් කරනකොට බලුම තව tuts කියවල :)
     

    pga

    Well-known member
  • Oct 4, 2006
    3,965
    914
    113
    %d and %p expect two arguments. but you provided only 1.

    printf("%d live in %p\n",num);
    printf("%d live in %p\n",num2);

    gcc test.c
    test.c: In function ‘main’:
    test.c:8:5: warning: format ‘%p’ expects a matching ‘void *’ argument [-Wformat=]
    printf("%d live in %p\n",num);
    ^
    test.c:9:5: warning: format ‘%p’ expects a matching ‘void *’ argument [-Wformat=]
    printf("%d live in %p\n",num2);
    ^

    should be corrected as,

    printf("%d live in %p\n",num,&num);
    printf("%d live in %p\n",num2,&num2);
     
    Last edited:
    • Like
    Reactions: GT9

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    %d and %p expect two arguments. but you provided only 1.

    printf("%d live in %p\n",num);
    printf("%d live in %p\n",num2);

    gcc test.c
    test.c: In function ‘main’:
    test.c:8:5: warning: format ‘%p’ expects a matching ‘void *’ argument [-Wformat=]
    printf("%d live in %p\n",num);
    ^
    test.c:9:5: warning: format ‘%p’ expects a matching ‘void *’ argument [-Wformat=]
    printf("%d live in %p\n",num2);
    ^

    should be corrected as,

    printf("%d live in %p\n",num,&num);
    printf("%d live in %p\n",num2,&num2);

    හම්මෝ ඇති යන්තන් මචං. :P ප්‍රශ්නේ විසඳුනා වගේම අඩුපාඩු කීපයකුත් හදාගත්ත. දැන් ඉස්සරහට තියෙන දේවල් කරගත්ත හැකි. ගොඩක් ස්තුතියි. :):):) rep+