TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Size of a pointer to an array of char (http://forums.techarena.in/software-development/1258242.htm)

Zool 14-10-2009 11:56 AM

Size of a pointer to an array of char
 
Code:

main ()
  (
      char text [128] = "Hi";
      cout <<sizeof (text) <<endl;

      char * textP = text;
      cout <<sizeof (textP) <<endl / / First attempt
      cout <<sizeof (* textP) <<endl; / / Second attempt
  )

Code:

Console : 128
  4
  1

How do I find the size of my table, since the pointer? Thank you for your help

Zecho 14-10-2009 11:59 AM

Re: Size of a pointer to an array of char
 
You can not. 128 is given by the compiler because it knows that the variable text is char [128] (ie the size is an integral type). By cons, textP variable is a pointer and the compiler does not know more, so anything that can give you is the pointer size (4 in your case) or the type to which it points (one char - 1 in your case).

Walby 14-10-2009 12:01 PM

Re: Size of a pointer to an array of char
 
In the first case, array of 128 char (1 char = 1 byte), size of 128 bytes. (but it's possible that it varies compilers I think).

In the second, you textP explicitly declared as a pointer to char. But as everyone knows, a pointer will always be a memory address. That is an integer, ie 4 bytes.

In the latter case, you give to textP * sizeof, ie a char ... But a tank that is 1 byte.
Code:

          main ()
  (
    char text [128] = "Hi";
    char * textP = malloc (128 * sizeof (char));
    court <<sizeof (text) <<endl / / displays 128
    court <<sizeof (textP) <<endl / / displays 4
  )


Aberto 14-10-2009 12:05 PM

Re: Size of a pointer to an array of char
 
A small mistake on line pointer that is "char * textP" and not "char * textP" By cons. Its meaningless reference to an array ...int & array [3];
Code:

template <int N> void f (const char (& array) / / There was the table size with size of (array), or N * size of (char)) char array [128] f (table);


All times are GMT +5.5. The time now is 02:52 AM.