0
#include <stdio.h>
#include <cs50.h>

void eatcandy(char a[])
{
    printf("%i", sizeof(a));             // **LINE 1:**
}

int main(void)
{
    char chocolate[] = "tasty";
    printf("%i", sizeof(chocolate));    // **LINE 2:**  
    eatcandy(chocolate);
    return 0;
}

I am trying to understand pointers using this sample program.

I know that the name of the array is the pointer to array's first element but what I don't understand is when I execute line 1 in code ,it shows the output as 8 bytes(since the memory allocated to a pointer in memory is 8 bytes for 64 bit OS) and when I execute line 2 in code , it shows the output as 6 bytes (5 bytes for "tasty" input and 1 byte for '\0').

I don't understand why line 2 doesn't show 8 bytes as line 1.

1 Answer 1

0

Arrays are arrays and pointers are pointers, but the name of the array works like a pointer to it's first element. Why did I say works like ? One of the reasons is exactly this:

  • sizeof pointer => is 8 bytes on 64bit sys
  • sizeof array => is the capacity of the array * size in bytes of the type of the elements the array is composed of. (so int array[5]; sizeof array; should return 20 bytes and char array[5]; sizeof array; should return 5 bytes)

People that implemented C just decided that it should be this way.

But when you pass an array into a function, the same people that implemented C decided that the array should decay to a pointer.

So in main(), the name a is an array. Inside the function, the name a has decayed to a pointer.

Maybe this helps:

2.2: But I heard that char a[] was identical to char *a.

Not at all. (What you heard has to do with formal parameters to functions; see question 2.4.) Arrays are not pointers. The array declaration "char a[6];" requests that space for six characters be set aside, to be known by the name "a." That is, there is a location named "a" at which six characters can sit. The pointer declaration "char *p;" on the other hand, requests a place which holds a pointer. The pointer is to be known by the name "p," and can point to any char (or contiguous array of chars) anywhere.

As usual, a picture is worth a thousand words. The statements

char a[] = "hello";   
char *p = "world";

would result in data structures which could be represented like this:

     +---+---+---+---+---+---+    
  a: | h | e | l | l | o |\0 |  
     +---+---+---+---+---+---+  
     +-----+     +---+---+---+---+---+---+    
  p: |  *======> | w | o | r | l | d |\0 |  
     +-----+     +---+---+---+---+---+---+

It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location "p," fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently. (See also questions 17.19 and 17.20.) https://www.lysator.liu.se/c/c-faq/c-2.html#2-2

If this answers your question, please click the check mark to accept.

6
  • But the name of the array still acts as a pointer to first element even before passing it as an argument to a function, right?
    – qwerty
    Commented Nov 30, 2016 at 12:48
  • The name of the array becomes a pointer inside the function. It's not simply acting as a pointer anymore, it literally becomes a true pointer by decaying. But outside the function, in main or whatever function called it, the array is an array, not a pointer. The name just works like a pointer, as I've said. Commented Nov 30, 2016 at 12:49
  • okay ,consider the example char a[5]="hello" ; printf("%i",sizeof(a)); what I can't understand is the output shows 6 bytes which is the size of actual array instead of size of pointer(8 bytes).
    – qwerty
    Commented Nov 30, 2016 at 12:50
  • Have you read my answer? I explained exactly why: sizeof an ARRAY returns its capacity. sizeof POINTER returns 8 bytes. You're asking the same question again. The array's name works like a pointer for a lot of things, but not ALL. The sizeof operation is exactly one of those things that differentiates arrays from pointers. Commented Nov 30, 2016 at 12:55
  • Okay.I think I got it now but what is the difference between a and &a if a is the name of array?
    – qwerty
    Commented Nov 30, 2016 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .