Create Array of Int - C++

Pages: 12
the only thing different between char and int is that cout / stream ops are overloaded to make them special snowflakes. You are all correct, depending on where you want to stand near the line of distinction. Its like saying a pointer is an int. It is, but its also not, depending on how much you want to get fired up about the topic... meh. I will leave that stuff to the lawyers that write the standards and spend their time nerfing unions.

g++ x.cpp -O3 -Wall -Wextra -pedantic -std=c++17
does not throw any errors or warnings for

int main()
{
int hmm{'a'};
cout << hmm;
}
good enough for me.
Last edited on
The type int is always a standard signed integer type;
the type char may be an unsigned integer type

The type signed char is a standard signed integer type
The type unsigned char is a standard unsigned integer type

The type char is a distinct type that has an implementation-defined choice of “signed char” or “unsigned char” as its underlying type. https://eel.is/c++draft/basic.fundamental#7
The type char is a distinct type

Exactly, and that is the distinction in this case between an int and a char.
A simple example of implementation-defined is the meaning of multiplying 2 integers vs multiplying 2 characters or multiplying 2 bools.

Also: https://en.cppreference.com/w/cpp/language/types
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   string myname;
   cin >> myname;
   vector<int> myints( myname.begin(), myname.end() );
   for ( auto e : myints ) cout << e << ' ';
}


lastchance
108 97 115 116 99 104 97 110 99 101 
@lastchance Might be right, looks convincing on face value ... but
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   string myname;
   cin >> myname;
   vector<int> myints( myname.begin(), myname.end() );
   for ( char e : myints ) cout << e << ' ';
}



dfg
d f g
Program ended with exit code: 0


or,

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   string myname;
   cin >> myname;
   vector<int> myints( myname.begin(), myname.end() );
   for ( int e : myints ) cout << e << ' ';
}



dfg
100 102 103 
Program ended with exit code: 0


The problem here is that 'auto' doesn't demonstrate equivalence of type 'auto' is used as a place holder - see https://en.cppreference.com/w/cpp/language/auto

There are numerous guidelines, opinions and comments indicating that auto must be used very carefully eg https://stackoverflow.com/questions/6900459/the-new-keyword-auto-when-should-it-be-used-to-declare-a-variable-type

All of this is consistent with the earlier references and the nature of the inequivalence of fundamental types char and int.

It's a good point though as another item worthy of caution.
Hi @againtry,
I wasn't making any point about equivalence of types - I'm staying well out of that argument and keeping well away from the firing zone!

I just thought it was a pretty quick way to fill an "array" of integers.

I'm still wondering if this was actually what the OP wanted: he/she hasn't given any indication.


BTW, I've just discovered that python is pretty anarchic when it comes to types and multiple variables with the same name in the same block of code:
1
2
3
4
5
6
7
8
myname = "lastchance"
print( myname )

temp = myname
myname = []
for c in temp:
    myname.append( ord(c) )
print( myname )


lastchance
[108  97 115 116  99 104  97 110  99 101]

But javascript is worse.
Last edited on
@lastchance that's OK I interpreted it otherwise and despite your other (more noble @OP-oriented ) purpose it raised a question about the char/int that had me stumped. If it had disproved what I commented on I would have accepted it as my error. However, I find that a lot has been written about auto and hence my comment.

As a matter of choice I avoid auto. As it turns out my previous intuition not to use it now has a firm basis. Thanks for your unwittingly useful demo :)

( Either way, a char is an int is incorrect, just as
a pointer is an int
is also not correct. )
Topic archived. No new replies allowed.
Pages: 12