Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

arrays - Behaviour of Sizeof in C

I have learnt that when we pass the array name to sizeof, the name of the array does not decay to the pointer to base address. The code below verifies this fact by giving answer 10.

#include <stdio.h> 

int main(){  
    int arr[10];  
    printf("Size of array is %d" , sizeof(arr)/sizeof(int));  
    return 0;  
}

However when I run the code below, the answer comes 1. Irrespective of whether a dimension is written in prototype or not , the answer is 1. Why is it so ?

#include <stdio.h>

void dimension(int arr[]){  
    printf("Sizof array is %d" , sizeof(arr)/sizeof(int));  
}


int main(){  
    int arr[10];  
    dimension(arr);  
    return 0;  
}  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This signature

void dimension(int arr[])

is absolutely equivalent to

void dimension(int *arr)

See also Question 6.4


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...