|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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 |
#2
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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); |
![]() |
|
Tags: c program, cout, pointer |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |