Yes, you can assign pointers to each other. A pointer's value is a location in memory. When you do an assignment of `ptr1 = ptr2` the address stored in ptr2 overwrites the address stored in ptr1.Zoramon089 said:Hey quick question, can you simply assign pointers to other pointers or is there a special way to do it? For instance can I do
Code:obj* ptr1 = sdfsdf; // Some assigment obj* ptr2 = hrtrh; ptr1 = ptr2;
Oh... oh my.Linkhero1 said:I fixed it with this, but idk how to shorten it cuz it looks ugly
Code:if (_start != 'A') if(_start!= 'B') if(_start!= 'C') if(_start!= 'D') if(_start!= 'E') if(_start!= 'F') if(_start!= 'G') if(_start!= 'H') if(_start!= 'a') if(_start!= 'b') if(_start!= 'c') if(_start!= 'd') if(_start!= 'e') if(_start!= 'f') if(_start!= 'g') if(_start!= 'h') { cout << "Fail... "; exit(1); }
if ((_start < 'A' || _start > 'H') || (_start < 'a' || _start > 'h')) {
std::cout << "Fail... ";
exit(1);
}
Linkhero1 said:Fixed it...I was using Or logic instead of And
so it works now
Code:if (first!= 'A' && first!= 'B' &&...
yesZoramon089 said:Hey quick question, can you simply assign pointers to other pointers or is there a special way to do it? For instance can I do
Code:obj* ptr1 = sdfsdf; // Some assigment obj* ptr2 = hrtrh; ptr1 = ptr2;
this should work (i think). im not sure how c++ plays with ascii chars.Linkhero1 said:I fixed it with this, but idk how to shorten it cuz it looks uglyCode:lol
if ( _start < 'A' /* 65 */ || (_start > 'H' /* 72 */ && _start < 'a' /* 97 */ ) || _start > 'h' /* 104*/ )
return exit(1);
Yeah I really rushed through it, should have just kept messing around before I asked here. thanks nonetheless.deadbeef said:lol I completely missed that. I noticed your expression being wrong: first != 'A' || 'B' || ...
Characters can be implicitly converted into integers. Feel free to treat them as such when needed. This can be helpful in cases like this, when you are dealing with a series of consecutive letters.
Right, but I highly doubt Link's code is meant to be maintained for longdeadbeef said:This is true, but when suddenly E and C are no longer valid choices, then it could be argued that it is easier to maintain the code if you just have to remove those expressions. But I'm not necessarily making that argument.
char lowercaseLowerBound = 'a';
char lowercaseUpperBound = 'h';
char uppercaseLowerBound = 'A';
char uppercaseUpperBound = 'H';
if ((_start < lowercaseLowerBound || _start > lowercaseUpperBound) ||
(_start < uppercaseLowerBound || _start > uppercaseUpperBound)) {
std::cout << "Fail... ";
exit(1);
}
poweld said:Characters can be implicitly converted into integers. Feel free to treat them as such when needed. This can be helpful in cases like this, when you are dealing with a series of consecutive letters.
Yep, I guess I was just trying to keep it simple, not that that makes much sense now that I'm not tired.maharg said:Characters *are* integers, nothing more and nothing less. It probably sounds like splitting hairs, but distinctions like this are important and fundamental.
Zoramon089 said:Hey quick question, can you simply assign pointers to other pointers or is there a special way to do it? For instance can I do
Code:obj* ptr1 = sdfsdf; // Some assigment obj* ptr2 = hrtrh; ptr1 = ptr2;
int id=0;
int i;
vector<*myClass> theList;
while(true) // My program is looping but I don't feel like typing out everything else
{
if(theList.empty())
theList.push_back(&myClass(id));
else
{
printf("id %i", theList[0]->getId())
theList[0] = &myClass(id));
}
id -858993460
josephdebono said:I'm not understanding &myClass(id).
What exactly are you doing there?
If that's your instantiation of the class, it's running out of scope and getting destroyed so you're accessing invalid memory, thus why you're getting the random integer.
int id=0;
int i;
vector<*myClass> theList;
while(true) // My program is looping but I don't feel like typing out everything else
{
if(theList.empty())
{
myClass m = myClass(id);
theList.push_back(&m);
}
else
{
printf("id %i", theList[0]->getId())
myClass m = myClass(id);
theList[0] = &m);
}
Zoramon089 said:Yeah, reading more about it, I kind of thought that was the issue but I did this as well and got the same problem.
Code:code
}
Zoramon089 said:Having some pointer problems that I don't really understand. This is just a small snippet of my code, shortened for the sake of this post, but including everything relevant.
Code:int id=0; int i; vector<*myClass> theList; while(true) // My program is looping but I don't feel like typing out everything else { if(theList.empty()) theList.push_back(&myClass(id)); else { printf("id %i", theList[0]->getId()) theList[0] = &myClass(id)); }
So as you can see I have a class I created named myClass which takes in an int as a parameter and simply saves it into an int field in the class. It also has a function called getId() which simply returns that field. The problem here is that when I print out the int I get
Code:id -858993460
Which I don't understand
}
int id=0;
int i;
vector<myClass*> theList; // the star goes afterward to indicate a pointer of type myClass
while(true) // My program is looping but I don't feel like typing out everything else
{
if(theList.empty())
theList.push_back(new myClass(id)); // this allocates memory for and initiates an instance of myClass, and returns a pointer to it
else
{
printf("id %i", theList[0]->getId())
// theList[0] = &myClass(id)); // commented this line, since it doesn't make sense to me what you're even trying to do here.
}
josephdebono said:I'm editing after I post each reply so you may have missed some of what I'm saying. Now, I understand what you're doing but I don't know if you're doing this to learn or for an actual 'useful' task. In reality, I see no reason for you to storing the pointers there, so:
vector<myClass> theList;
theList.push_back(myClass(id));
theList[0].getId();
would work just as well and remove the unnecessary pointer headaches.
For future reference, if you're using dynamic_cast<> chances are you're doing somethign wrong. Keep going with it while you're still learning the language, but just keep it in the back of your mind that there's almost certainly a better way.Zoramon089 said:No, they need to be pointers for other functionality in the program. Mainly because myClass is a derived class of another class and sometimes I need to cast it as that based class. I can only use the dynamic_cast if it's a pointer
cpp_is_king said:For future reference, if you're using dynamic_cast<> chances are you're doing somethign wrong. Keep going with it while you're still learning the language, but just keep it in the back of your mind that there's almost certainly a better way.
Zoramon089 said:Yeah, i've tried a number of ways already. I even consulted this topic earlier and that ended up being the method that worked best. I basically have a vector of a base class and I need to add a bunch of derived classes to it, so I can loop through all of them and call a fucntion in the base class on all of them.
That works just fine but sometimes I need to call a function specific to a base class (which i can determine accurately based on the index in the vector). When i tried a simple cast of (derivedClass)list[index] it kept giving me errors about there being no way to cast to the derived class.
Yeah, and the dynamic cast was what ended up working!fenners said:Didn't we go over this earlier in the thread?
If you're just looking for alpha characters (a-zA-Z) you could just use istream and check the retrieved characters to see if they're <45 ('A') or >122 ('z')rexor0717 said:Hey people, I'm back again. I'm trying to find a good way to read in file, but have it exclude all whitespace and punctuation. I'm looking at istream right now, but I don't see a way that I can make it exclude punctuation. Do you know of something else I could use, or am I just not looking in the right place?
char c;
while(whatever) {
instream.get(c);
if (c >= 'A' || c <= 'z') {
// do what you will with c
}
}
Unfortunately, no. Do they host these often? I've never looked into it, to be honest.Lathentar said:Anyone do Google CodeJam last night/today? It was a lot of fun, 3/4 questions were video game references.
The last two were kinda mean on Google's part though. Couple of questions that appear very difficult at first yet ended up having super trivial answers.
Nice, thanks. Did the first two (really digging the references ) but I'm a little stumped by how to approach the 3rd.Lathentar said:Once a year. Yesterday was the qualifier round.
You can still do all the questions.
void functionA(const char* str, ...);
bool functionB(int,int,const char*,...);
bool functionB(int a,int b, const char* string,...){
//code
//call of functionA();
//return true;
}
poweld said:Nice, thanks. Did the first two (really digging the references ) but I'm a little stumped by how to approach the 3rd.
I know that for all order bits, there must be an even number of said bit (or 0) to keep the kid from crying. As for finding out the most lucrative combination for the smart kid, I'm guessing I just need to come up with a search tree that finds the most lucrative combination of numbers that contains an odd count for all order bits (that aren't 0)
But the way you made it sound, perhaps I'm overthinking it?
item_iterator m1;
pair<item_iterator, item_iterator> range = items.equal_range(item);
for (m1 = range.first; m1 != range.second; ++m1)
{
std::cout << "Directory Location: " << (*m1).second << endl;
}
No. However, you can pass the va_list.BlueMagic said:So I have 2 functions with the following prototypes:
Code:void functionA(const char* str, ...); bool functionB(int,int,const char*,...); bool functionB(int a,int b, const char* string,...){ //code //call of functionA(); //return true; }
(functionB uses functionA).
Is there any way I can directly pass the ellipsis arguments from functionB to functionA?
Thank you very much, I appreciate the help.
Complex Shadow said:Can someone explain me the diffrence between references and pointers.
A reference can never be reseated, but a pointer can.Complex Shadow said:Can someone explain me the diffrence between references and pointers.
int& someNumRef = someNum;
someNumRef = 5; // someNum is now equal to 5
int* someNumPtr = &someNum;
*someNumPtr = 10; // someNum is now equal to 10
someNumPtr = &someOtherNum;
*someNumPtr = 20; // someOtherNum is now equal to 20
poweld said:A reference can never be reseated, but a pointer can.
Code:int& someNumRef = someNum; someNumRef = 5; // someNum is now equal to 5 int* someNumPtr = &someNum; *someNumPtr = 10; // someNum is now equal to 10 someNumPtr = &someOtherNum; *someNumPtr = 20; // someOtherNum is now equal to 20
I like how you put it.maharg said:Put more in terms of *why* they're different rather than the specific details of how, a reference acts like a value. References also imply a lack of ownership over the underlying resource.
So basically, if you just want to avoid copying something when you pass it into a function (because it's heavy or has non-copying semantics), and intend to treat it as a value, you should use a reference.
Integers are 4 bytes long, so 150x4 = 600bytesZombie Lyle said:Hey, I'm looking over past papers for my C++ exam next week and can't find the answer for this question.
In C++ how may bytes of memory are occupied by an array of 150 integers and an array of 150 character pointers.
Thanks in advance.
You should be looking for places where you are looking at index positions in your string, for example when you use the bracket operator (e.g. myString[10]) or at (e.g. myString.at(10)). The error is being thrown because you're trying to access an index in the container that does not exist.Rapstah said:I'm getting std:ut_of_range errors concerning a string check function in my program. For everyone's sanity I'm not going to post the actual application here but what usually causes this? I have a ton of loops going through entire strings in the function in question - should I be looking for places where it tries to look at what character there is at a position that isn't defined in the string?
I figured that, good to know I'm at least thinking right at some level, ha.poweld said:You should be looking for places where you are looking at index positions in your string, for example when you use the bracket operator (e.g. myString[10]) or at (e.g. myString.at(10)). The error is being thrown because you're trying to access an index in the container that does not exist.
poweld said:Integers are 4 bytes long, so 150x4 = 600bytes
Characters are 1 byte, so 150x1 = 150bytes
Zombie Lyle said:Hey, I'm looking over past papers for my C++ exam next week and can't find the answer for this question.
In C++ how may bytes of memory are occupied by an array of 150 integers and an array of 150 character pointers.
Thanks in advance.
Yeah. A pointer is 32 bits (4 bytes) on a 32 bits architecture and 64 bits (8 bytes) on a 64 bits architecture.deadbeef said:Isn't this architecture dependent? Wouldn't it be more correct to use the sizeof operator?
Yeah, I guess I just figured if it's asking for the size of an int they may be assuming 32bit. Also, good eye, I didn't see that it was char pointers, not char's.Tigel said:Yeah. A pointer is 32 bits (4 bytes) on a 32 bits architecture and 64 bits (8 bytes) on a 64 bits architecture.
If the question is on a 32 bits architecture, then 150 integers and 150 char pointers use the same amount of memory (150 * 4 bytes).