Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-10-2009
Member
 
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
Reply With Quote
  #2  
Old 14-10-2009
Member
 
Join Date: May 2008
Posts: 2,293
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).
Reply With Quote
  #3  
Old 14-10-2009
Member
 
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 
  )
Reply With Quote
  #4  
Old 14-10-2009
Member
 
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Size of a pointer to an array of char"
Thread Thread Starter Forum Replies Last Post
Array of char and int Jensen Ackles Software Development 5 23-03-2010 09:50 AM
Problem in representing 3 x 3 array of char in c++ KAILEY Software Development 5 20-02-2010 08:05 PM
Comparing char array in c++ MAHAH Software Development 5 15-02-2010 08:19 PM
Randomaccessfile using char array TechGate Software Development 5 27-01-2010 11:19 AM
How do i clear char array in c++ B_Hodge Software Development 3 16-05-2009 09:35 AM


All times are GMT +5.5. The time now is 03:41 PM.