marcus.x的博客

常见sizeof 笔试题

最近面试过程中遇到了很多很多sizeof的问题。

现在总结一下:

#include <stdio.h>
#include <string.h>

void fun(int array[], char *str)
{
    printf("sizeof(array) : %d\n", sizeof(array));
    printf("sizeof(str) : %d\n", sizeof(str));
    printf("sizeof(str + 10) : %d\n", sizeof(str + 10));
}
int main()
{
    int arrar[100] = {0};
    int arr_second[100];
    char *string = "hello world!";
    char *second = string + 6;
    char *p = NULL;
    printf("sizeof(p=NULL) : %d\n", sizeof(p));
    printf("sizeof(arrar) : %d\n", sizeof(arrar));
    printf("sizeof(arr_second) : %d\n", sizeof(arr_second));
    printf("sizeof(string) : %d\n", sizeof(string));
    printf("sizeof(second) : %d\n", sizeof(second));
    printf("strlen(string) : %d\n", strlen(string));
    printf("strlen(second) : %d\n", strlen(second));
    fun(arrar, string);    
    
    return 0;
}

 

结果呢:如下

sizeof(p=NULL) : 4
sizeof(arrar) : 400  //整型占4个字节
sizeof(arr_second) : 400
sizeof(string) : 4 
sizeof(second) : 4
strlen(string) : 12
strlen(second) : 6
sizeof(array) : 4  //形参,数组被弱化为指针。
sizeof(str) : 4
sizeof(str + 10) : 4

 

posted on 2014-03-20 18:33  marcus.x  阅读(459)  评论(0编辑  收藏  举报

导航