Results 1 to 4 of 4

Thread: Size of a pointer to an array of char

  1. #1
    Join Date
    Apr 2009
    Posts
    87

    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

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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).

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    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 
      )

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    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);

Similar Threads

  1. Array of char and int
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:50 AM
  2. Problem in representing 3 x 3 array of char in c++
    By KAILEY in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 08:05 PM
  3. Comparing char array in c++
    By MAHAH in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 08:19 PM
  4. Randomaccessfile using char array
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 11:19 AM
  5. How do i clear char array in c++
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 16-05-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,537,369.42430 seconds with 16 queries