sizeof operator

I ran across this code on a web tutorial. I am curious about the use of the sizeof operator whichis being used to determine the size of an array:

[code]
#include
#include

char answer[100], *p;
printf("Type something:
");
fgets(answer, sizeof answer, stdin);
if((p = strchr(answer, '
')) != NULL)
*p = '';
printf("You typed "%s"
", answer);
[/code]

Why would this return the size of the array, and not the size of type pointer to char?

Comments

  • [b][red]This message was edited by stober at 2005-6-20 17:5:34[/red][/b][hr]
    : I ran across this code on a web tutorial. I am curious about the use of the sizeof operator whichis being used to determine the size of an array:
    :
    : [code]
    : #include
    : #include
    :
    : char answer[100], *p;
    : printf("Type something:
    ");
    : fgets(answer, sizeof answer, stdin);
    : if((p = strchr(answer, '
    ')) != NULL)
    : *p = '';
    : printf("You typed "%s"
    ", answer);
    : [/code]
    :
    : Why would this return the size of the array, and not the size of type pointer to char?
    :
    :


    because answer is an array, not a pointer. the sizeof operator always returns the number of bytes allocated 5o the object. In the example, answer was allocated 100 bytes so sizeof(answer) = 100. Had answer been a pointer, like char* answer, then the sizeof operator would have return the size of the pointer.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion