arraysize seems to be wrong?

M

MCP

Hello,

I ran the following simple test:


#define ARRAYSIZE(a) (sizeof(a)/sizeof(char))

#include <stdio.h>
#include "chrutil.h"

int main(void) {

char *array = "12345";
char *p = array;

printf("Arraysize: %d\n", ARRAYSIZE(array));

while (*p != '\0') {
printf("Digit: %d\n", dig_to_int(*(p++)));
}

return 0;
}

int dig_to_int(char c) {

if (IS_DIGIT(c)) {
return c - '0';
}
printf("Error: %c\n", c);
}

In this case, the macro ARRAYSIZE or even the simple function sizeof
shows me that "array" has a size of 4?

When I change the line with the array to char array[] = "12345"; it
suddendly shows me that "array" has a size of 6?
How can one explain that?

Regards,
Markus
 
D

Default User

MCP wrote:

In this case, the macro ARRAYSIZE or even the simple function sizeof
shows me that "array" has a size of 4?

When I change the line with the array to char array[] = "12345"; it
suddendly shows me that "array" has a size of 6?
How can one explain that?

Your question is a variant of one of the FAQs:

<http://c-faq.com/aryptr/aryparmsize.html>




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top