雖然沒有禮品,希望大家能看看這幾個關於指標的小題目

來源:互聯網
上載者:User

還是和指標、記憶體相關,最近又看了林銳先生的文章,寫了一些代碼在這裡,大家如果感興趣,可以看看,答案都是什嗎?

程式是用vc6+sp6編譯的,有編譯問題、有運行時問題,呵呵。。。

Q1-Q8,都輸出什麼內容???

(暫時放首頁幾天,到時候撤下來)

#include "string.h"

#include "stdlib.h"

char* GetName(void){

    char name[20] = "juqiang";

    return name;

}

int GetSize(char buf[]){

    return sizeof(buf);

}

int main(int argc, char* argv[])

{

    char buf[] = "hello";

    printf("Size of buf is:%d\r\n",sizeof(buf));// Q1

    printf("Size of buf calling is:%d\r\n",GetSize(buf));// Q2

    printf("Hello %s\r\n",GetName());// Q3

 

    char* p = (char*)malloc(100);

    int paddr = (int)p;

    free(p);

    int paddr2 = (int)p;

    printf("paddr equals paddr2 is:%d\r\n",paddr==paddr2);// Q4

    if(NULL != p){

         strcpy(p,"hello!");

    }

    printf("The contents of p is:%s",p);// Q5

   

    char name[] = "juqiang";

    name[0] = 'J';

    name[1] = 'U';

    printf("Name is:%s\r\n",name);// Q6

   

    char bufname[] = "juqiang";

    char* name2 = bufname;

    *(name2+0)='J';

    *(name2+1)='U';

    printf("Name is:%s,%s\r\n",bufname,name2);// Q7

 

    char* name3 = "juqiang";

    *(name3+0) = 'J';

    *(name3+1) = 'U';

    printf("Name is:%s\r\n",name3);// Q8

 

   return 0;

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.