1
int main()
{
    struct
    {
        char *name_pointer;
        char all[13];
        int foo;
    } record;

    printf("%d\n",sizeof(record.all));
    printf("%d\n",sizeof(record.foo));
    printf("%d\n",sizeof(record));

    return 0;
}

I want the size of the pointer vatiable "*name_pointer" in the structure....

5
  • 1
    read: What does sizeof(&arr) return? to know differences between name_pointer and all. Commented Aug 1, 2013 at 11:57
  • 1
    sizof(char*); would tell you Commented Aug 1, 2013 at 11:57
  • 1
    sizeof(record.name_pointer); Commented Aug 1, 2013 at 11:57
  • 3
    you want sizeof(record.name_pointer) or strlen(record.name_pointer) ? Commented Aug 1, 2013 at 11:57
  • 4
    what stopped you from sizeof(record.name_pointer); Commented Aug 1, 2013 at 11:59

4 Answers 4

7

To get the size of the pointer use

printf("%d\n", (int) sizeof(record.name_pointer));

Your might get 2, 4, 6, 8, etc.


To get the size of the data that is pointed to by the pointer (a char) use

printf("%d\n", (int) sizeof(*record.name_pointer));

Your should get 1.


To get the string length of the string pointed to by the pointer, assuming record.name_pointer points to legitimate data, use

printf("%d\n", (int) strlen(record.name_pointer));

BTW As @alk says and why the (int) casting above, a suitable conversion specifier to use with sizeof() includes the 'z' prefix. The result of sizeof() and strlen() is of type size_t. Although size_t and int are often the same, there are many systems where they are of different sizes. And since sizeof() is an "unsigned integer type" (C11 6.5.3.4), I recommend

printf("%zu\n", sizeof(...
Sign up to request clarification or add additional context in comments.

5 Comments

in the second one - data being pointed is a string - not the first character. so this is wrong.
Nitpick: As strlen() returns size_t the suitable conversion specifier should be prefixed by the appropriate length modifier z to become zd.
@ManojAwasthi: name_pointer is defined as char *, so it points to exactly one character. So everything is fine.
@alk Yes - I concur - thought of that but was uncertain to add that improvement to a basic question.
@Manoj Awasthi record.name_pointer is a pointer to a char, thus the sizeof(*record.name_pointer) will be 1. As a string in C is a variable length array of characters and the string value is passed about by the address of the first char, whether record.name_pointer is used to point to only 1 char, a string, or a fixed char array is indistinguishable from OP's code snippet.
0

Pointer variable will always(32 bit system architecture) have size 4. if you have 64 bit system architecture it is 8.

2 Comments

remove always add in your system
Not on 64 bit systems. It's 8 there. And there are others ...!
0

I believe it is a GOOD idea to use data types in sizeof rather than variables for native data types. so use:

sizeof(char *)

1 Comment

Really? I prefer to use the variable, so code usually remains correct if you change the variable type.
-1

use the following code:

printf("%d\n", (int) sizeof(record->name_pointer)/4);

2 Comments

It would be more helpful to provide an explanation along with a working solution.
This won't work. record isn't a pointer so you can't dereference it with ->. In addition, why are you dividing by 4?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.