UPDATE (Nov 26):
Through more indepth analysis! Finally figured out what it means! At least I hope it's really what it means, but seeing how it trends with other types, I think it's correct!
A5_c - means it's an array of 5 char type elements
PA5_c - means it's a pointer to the array of 5 char type elements
Pc - means pointer that holds an address to another variable
Praise God!
Hello everyone!
I recently started to learn cpp on my own and is learning the concept of pointers, which I hear is a very challenging concept, so I wanted to practice until I fully understand it. The part I am on is about arrays and pointers and through using the mac terminal and the GNU compiler, I wrote a simple program like this and also the results can be found in the comments, could someone help me understand what A5_C, PA5_C, and Pc mean? And also please correct me if anything i thought i understood is incorrect :). Thank you in advance!
#include <iostream>
#include <string.h>
#include <typeinfo>
int main() {
char name[] = "Lily";
//prints size of char array which is 5 bytes in this case
std::cout << sizeof(name) << std::endl;
//------------------------------------------------------
//this prints Lily which is of type: A5_C (huh?????????)
std::cout << name << " which is of type: " << typeid(name).name() << std::endl;
//------------------------------------------------------
//this prints 0x7ffee6425b07 which is of type PA5_C
//(is PA5_C meaning address?)
std::cout << &name << " which is of type: " << typeid(&name).name() << std::endl;
//------------------------------------------------------
//this prints L which is of type c
//which I understand because it's dereferencing the pointer
//and getting me the content that's occupying the address
std::cout << *name << " which is of type: " << typeid(*name).name() << std::endl;
//------------------------------------------------------
//this prints ily which is of type Pc
//I am assuming this means pointer type but
//then why is it significantly more different than type of name?
std::cout << (name+1) << " which is of type " << typeid(name+1).name() << std::endl;
//------------------------------------------------------
//this prints i which is of type c
//which makes sense, because you can do
//arithmetics with pointer variables and this adds
//1 byte to name so it gives me
//the value in the address 1 unit above
std::cout << *(name+1) << " which is of type " << typeid(*(name+1)).name() << std::endl;
//------------------------------------------------------
//this prints _blank which is of type c
//and makes sense but it's very bad because
//c++ have no bounds checking for arrays and
//so this address might store some garbage value
std::cout << *(name+5) << " which is of type " << typeid(*(name+5)).name() << std::endl;
return 0;
}
Please excuse the messy commenting, but hopefully it's clear!!
Top comments (0)