This programme shows only one output “Hiii” if the size of the words array is not passed from the main function.
If size is generated inside disp function shows
Size: 48
Size: 2
Hiii
If size is passed from main function shows
Size: 48
Size: 48
Hiii
Hello
Hello There
Hello World
#include<stdio.h>
#include<conio.h>
void disp(char words[][12],int size)
{
int i,j;
char *p,*cp;
p=words;
size=sizeof(words);
printf("Size %d\n",size);
for(cp=p;cp<(p+size);cp+=12)
{
printf("%s",cp);
printf("\n");
}
}
void main()
{
char words[][12]={"Hiii","Hello","Hello There","Hello World"};
int size;
clrscr();
size=sizeof(words);
printf("Size %d\n",size);
disp(words,size);
getch();
}
In C, the syntax for passing an array is actually just a another way of saying you are passing a pointer. For example:
is just another way of saying
It is a special syntax in the language, and it is meant to make the parameters more self documenting, but it can also be confusing in situations like what you are describing.
In your case, the declaration:
is actually saying
That is, words is a pointer to an array of 12 characters. And the size of a pointer is two bytes in your case.