malloc和calloc重新分配內存

#include <stdio.h>
#include <stdlib.h>

void fun(int* p1, int* p2, int* s)
{
    s = (int*)malloc(sizeof(int));
    //s = (int*)calloc(1,sizeof(int));
    *s = *p1 + *(p2+1);
}

int main()
{
    int arr1[2] = {1, 2}, arr2[2] = {10, 20};
    int* s = arr2;
    fun(arr1, arr2, s);
    printf("%d\n", *s);
}

//由於malloc或者calloc會重新分配內存所以輸出不等於21
//最後輸出應該等於10

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章