c - Why do I get a different value of "a". To get the same value what needs to be done? -


this question has answer here:

#include<stdio.h> void func(int x[]); main() {     int a[]={1,2,3,4};     printf("size of %d \n",sizeof(a));  // value i'm getting     func(a); } void func(int a[])  {     printf("size of %d",sizeof(a));  // value changing } 

both times, value of 'a' not printing same. same value maintaining code, more code need added or changes required?

i don't want change signature of function. without changing signature, code needed added inside func(int a[])?.

an array function argument decays pointer, meaning argument func of type int*. can therefore calculate sizeof(int*) inside func.

if want pass array size, can either pass separate argument

func(a, sizeof(a)/sizeof(a[0])); .... void func(int* a, int num_elems); 

or initialise a include sentinel value marking array end , iterate through elements until find value in func.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -