About Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sridevi101
    New Member
    • Aug 2009
    • 1

    About Arrays

    main()
    {
    char *str [] ={"Frogs","Do", "Not","Die.","T hey","Croak"};
    printf("%d%d",s izeof(str),size of(str[0]));
    }

    Please let me know the answer ....
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Try compiling and running it yourself.

    If (when really) you get compiler errors if you don't understand them please feel free to post back here.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      printf("%d\n",s izeof(str));//prints 20 because of '.' after Die and NULL char.
      printf("%d\n",s izeof(str[0]));prints 4
      printf("%d",siz eof(str[5]));prints 4
      Remember array size is zero based and NULL will not print.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No sizeof(str) is not 20, remember that str is an array of char pointers so size of str is dependent on the size of char pointer and the number of items in the array. What it is not dependent on is any of the contents of the string pointed to.

        sizeof(str[0]) and sizeof(str[5]) might be 4 on a 32 bit platform that has 32 bit pointers.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          yes...I should have said that those byte sizes applied to the machine I was using.........t hanks for correcting.

          Comment

          Working...