how to iterate through char array

i have associated a pointer to a char array. I would like to print each element of the char array using the dereference pointer notation. for some reason it would only display the first letter of each element in the char array.

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
# include <iostream>

using namespace std;

int main()
{
	char loaded2[] = { 'aa', 'BB' }; 

	//char * p = test;

	char * pp = loaded2;

	cout << *pp << endl;

	++pp;

	cout << *pp << endl;



	for (char * p = loaded2; p != loaded2 + sizeof(loaded2) / sizeof(loaded2[0]); ++p)
	{
		
		cout << *p << endl;
	}
  

	system("pause");


}  



'x' a character
"x" a string
'xx' an abomination
https://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters
An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-deļ¬ned value.
char arrays that represent C style strings end in zero.
so
"xx" is really 'x' 'x' '\0' ... 3 chars long
which allows iteration of

for(I = 0; str[I]!= 0; I++)
something(str[I]);

but 99% or more of anything you would want to do to a C string is done with C string functions that do loops like the above for you internally.

better to learn
string x; //c++ way

but that is how you do it.

closed account (48T7M4Gy)
Notwithstanding pointer horror show:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include <iostream>

using namespace std;

int main()
{
    char* loaded2[] = { "aa", "BB", "CC" };
    char ** pp = loaded2;
    
    cout << *pp << endl;
    ++pp;
    cout << *pp << endl;
    
    char** qq = loaded2;
    for (int i = 0; i <  sizeof(loaded2)/sizeof(char*); ++i)
    {
        cout << *qq << endl;
        ++qq;
    }
}


aa
BB
aa
BB
CC
Program ended with exit code: 0
Sorry i am somewhat confused. why am i declaring a pointer to a pointer.

if I derefence a pointer to a pointer ie *qq , should i not get a hexadecimal value , how is that cout displays "aa" ?

when i look in my debugger i can see that:

qq is described as a hex. number {a hex. number "aa"}
while *qq is described as a hex. number "aa"

and **qq is 97'a' . a single letter
closed account (48T7M4Gy)
A pointer to a pointer arrangement is required because you have an array of strings (char*'s)

A single C-style string is char* (ie an array of characters), while an array of C-style strings is an array of char*'s hence char**.

If you are having a lot of trouble with this, and most people do, then if you can use C++ strings as has already been commented.
closed account (48T7M4Gy)
PS
And if you dereference a char** you get a char* which is effectively a C-style string. (Forget about hex because that aspect is not relevant here)
so why is that when i declare a pointer to a char* i am only able to get the first letter , however if i declare a pointer to a pointer , i am able to get the full word ?
a coding error.

look.

char * cp = new char[100];
cp[0] = 0; //make it the empty string.
strcat(cp, "hello world and stuff");

cout << cp; //you should get all the text assigned above.

cout << cp[0] << *cp; //you get the letter h, twice.

closed account (48T7M4Gy)
so why is that when i declare a pointer to a char* i am only able to get the first letter , however if i declare a pointer to a pointer , i am able to get the full word ?


The answer to this is yet another example of the horror of c-style strings. The short answer is the use of single ' vs double " quotation marks.

Single quotes are a single character ie an array of characters with one and only one element, double a string ie an array with one or more than one character forgetting about empty strings/characters.

So 'aa' is not only incorrect syntax, it hides the obscurity what is going on even more.

All of that is why my line 7 is different to your line 7 (aside from the obvious number of array elements)

closed account (48T7M4Gy)
so why is that when i declare a pointer to a char* i am 
only able to get the first letter , however if i declare a pointer to a pointer , 
i am able to get the full word ?


Go to your line 7 single quotes mean a character, double quotes means a C-style string. That's why I used double quotes.

Last edited on
asd
Topic archived. No new replies allowed.