Address of array data type?

Hi guys,

Lets say we have this short code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main()
{
	char abc[5];
	strcpy(abc, "ab");

	printf("%p\n", &abc);
	printf("%p\n", abc);


	printf("%p\n", &abc +1);
	printf("%p\n", abc +1);
	
}


sample Output:
006FF7B0
006FF7B0
006FF7B5
006FF7B1

I was wondering, what datatype is &abc because compiler complains about
char ** p = &abc; //err

Also why is it that &abc +1 points to the end of array but &abc to the start?
&abc is a pointer to an array of 5 chars. So ++/ +1 will increment the pointer address by the size of the whole array (i.e. by 5 chars.)

char (*parray)[5] = &abc;

Whereas abc is the address of the array and &abc[0] the address of the first array element.

When you write

char* pch = abc;

you are relying on array decay, which means it's equivalent to

char* pch = &abc[0];

Andy
Last edited on
If you compile C++, you can get the compiler to tell you the type.

Andy

Output when compiled using MinGW GCC (32-bit)

type of &abc    is char (*) [5]
type of abc     is char [5]
type of &abc[0] is char*
type of abc[0]  is char

0023ff47
0023ff47
0023ff47

0023ff4c
0023ff48
0023ff48


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <cstdio>
using namespace std;

#ifdef __GNUG__
#include <cxxabi.h>
#endif

// GCC returns 'mangled' names from std::typeid::name so need to convert them
// to a meaningful name.
//
// "Borrowed" from:
// Unmangling the result of std::type_info::name
// http://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname
std::string demangle(const char* name);
template <class T>
std::string type(const T& t) {
    return demangle(typeid(t).name());
}

int main()
{
	char abc[5] = "ab"; // initializes array with 'a' and 'b' in first 2 elems
                            // and zeros the rest

	char (*parray)[5] = &abc; // pointer to array of 5 chars

	char* pch1 = abc;     // array address decays to pointer to first element
	char* pch2 = &abc[0]; // get address of first element explicitly

	cout << "type of &abc    is " << type(&abc)    << "\n";
	cout << "type of abc     is " << type(abc)     << "\n";
	cout << "type of &abc[0] is " << type(&abc[0]) << "\n";
	cout << "type of abc[0]  is " << type(abc[0])  << "\n";
	cout << "\n";

	printf("%p\n", &abc);
	printf("%p\n", abc);
	printf("%p\n", &abc[0]);
	printf("\n");

	printf("%p\n", &abc + 1);
	printf("%p\n", abc + 1);
	printf("%p\n", &abc[0] + 1);
	printf("\n");

	return 0;
}

#ifdef __GNUG__
struct handle {
    char* p;
    handle(char* ptr) : p(ptr) { }
    ~handle() { free(p); }
};

std::string demangle(const char* name) {
    int status = -4; // some arbitrary value to eliminate the compiler warning
    handle result( abi::__cxa_demangle(name, NULL, NULL, &status) );
    return (status == 0) ? result.p : name ;
}
#else
// does nothing if not g++
std::string demangle(const char* name) {
    return name;
}
#endif 

Last edited on
Ok, it`s more clear now, Thank you for the input Andy.
Topic archived. No new replies allowed.