Clarification needed on Pointers/Arrays

S. Manoharan sam at lfcs.ed.ac.uk
Tue Feb 21 06:59:17 AEST 1989


To clarify my doubts regarding pointers/arrays, I ran the
following code. Result: I am more confused. Could someone
help me out?

I need to reason the likely outcome of 
   /* LINE 1 */  and /* LINE 2 */ 

main()
{
   static char *a[] = { "123456789", "bull", "fred", "foo" };
   /* array of char-pointers */

   printf("Entries %d\n",sizeof(a)/sizeof(char *));
   foo1(a);
   foo2(a);
}

foo1(b)
char *b;
{
   int i;

   printf("Entries %d\n",sizeof(b)/sizeof(char *));
   /* LINE 1 */ for ( i = 0; i < 10; ++i ) printf("%d: %c\n",i,b+i);
}

foo2(b)
char *b[];
{
   int i;
   /* LINE 2 */ printf("Entries %d\n",sizeof(b)/sizeof(char *));
   for ( i = 0; i < 4; ++i ) printf("%d: %s\n",i,b[i]);
}

--------
Output of the program was:

Entries 4
Entries 1
0: ^P              <--- Expected ... 1
1: ^Q              <--- Expected ... 2
2: ^R              <--- Expected ... 3
3: ^S              <--- Expected ... 4
4: ^T              <--- Expected ... 5
5: ^U              <--- Expected ... 6
6: ^V              <--- Expected ... 7
7: ^W              <--- Expected ... 8
8: ^X              <--- Expected ... 9
9: ^Z
Entries 1          <--- Expected ... 4
0: 123456789
1: bull
2: fred
3: foo
---
sam%uk.ac.edinburgh.lfcs%uk.ac.ucl.cs.nss at net.cs.relay



More information about the Comp.lang.c mailing list