-1

Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)

sample code written in c

#include<stdio.h>

int main()
{
    char a[]="Visual C++";
    char b[] = "Visual\C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}

OUTPUT

11,10

1,1

1.) Why sizeof(a) shows 11, although, it has only 10 characters.

2.) Why sizeof(a) and sizeof(b) showing different output, although, they have same number of characters.

Community
  • 1
  • 1
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Don't forget the null character... And the backlash `\\` is an escape character. – Mysticial Nov 28 '12 at 18:36
  • which means.. if we don't know the size of the array, and we have to iterate whole elements of the array. so on that case, will have to move till **`(sizeof(a)-2)`** isn't ?? – Ravi Nov 28 '12 at 18:39
  • `sizeof(a)` will give you the size of the array. (and also the length of the array for type `char`). `strlen(a)` will give you the number of characters before the first null character. – Mysticial Nov 28 '12 at 18:44
  • `'\C'` is undefined behavior, and you should use a more appropriate conversion specifier for the type `size_t`: `%u` (better with a cast) in C89, `%zu` from C99. – effeffe Nov 28 '12 at 19:28

6 Answers6

3

Because there is a null (\0) at the end of the string and \C is interpreted as a single character.

If you want the backslash to show in a C string you must escape it using another backslash.

char b[] = "Visual\C++";  \\ 10 characters including the null terminator
char b[] = "Visual\\C++"; \\ 11 characters including the null terminator
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
0

1 - becuase the string has a 00 byte (aka null terminator) on it thats included in the sizeof

2 *a is the first character in the string - size of 1 char is 1

pm100
  • 48,078
  • 23
  • 82
  • 145
0

a contains 10 characters, plus the terminating null ('\0') character; hence the size is 11.

And a and b do not contain the same number of characters. In b, the \C part is considered an escape sequence, and is counted as one character. If you want the \ to appear, use "Visual\\C++".

Praetorian
  • 106,671
  • 19
  • 240
  • 328
0

Point 1:

"Visual C++" length = 11 /* including null character */

"Visual\C++" length = 10 /* including null character and '\C' is interpreted as one character.

Point 2:

*a means you are accessing the first character.

*a <=> a[0] both are same.

a[0] is 'V' in your case whose length is 1.

Adeel Ahmed
  • 1,591
  • 8
  • 10
0

Look at it from a character stand point:

char a[]="Visual C++";

printf("length = %d\n", strlen(a));
for(int i = 0; i<strlen(a); i++)
    printf("a[%d] = %c (ASCII %d)", i, a[i], a[i]);

With code like this, you'll get

length = 11
a[0] = V (86)
a[1] = i (105)
a[2] = s (115)
a[3] = u (117)
a[4] = a (97)
a[5] = l (108)
a[6] =   (32)
a[7] = C (67)
a[8] = + (43)
a[9] = + (43)
a[10] =  (0)

Checking those values against an ASCII Table you can see why it shows 11 (the NULL terminator)

char b[] = "Visual\C++";

Your second string has an escape char in it \, there's lots of lists of them, but it basically tells the compiler to ignore the next character because it's not to be printed, but something special. Just like the newline character: '\n'


Based on your comments on the original post, I think I need to clarify two extra things:

Extra Note 1: A special char, such as newline '\n' or null terminator '\0' only takes 1 extra byte of space.

Extra Note 2: sizeof(a) will give you the size (number of characters) of your array because it's full of characters which only take 1 byte each. When you're using this on other types that take up more space you need to do one more step:

int arr[4] = {0};
int size_of_arr = sizeof(arr/sizeof(int));
Mike
  • 47,263
  • 29
  • 113
  • 177
0

In the linux kernel that is the macro:

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
0x90
  • 39,472
  • 36
  • 165
  • 245