How to save more pointers in one array - c++

I want to make a pointer to an instance of a class. Many instances - that is why I made an array which saves all of those.
But how can I set the value of a pointer in a class to 0?
That is the code... Maybe you know what I'm talking about
public:
CCharacter *pTeamMember[15];
And in another file:
pTeams[team]->pTeamMember = 0;
It causes following error.
error C2440: '=' can't convert 'int' into 'CCharacter *[15]
What I don't understand is, that this don't causes any errors:
public:
Team *pTeams[31];
And in another file:
pTeams[i] = 0;
Does anyone have ideas?

pTeamMember isn't a pointer. It's an array of pointers-to-CCharacter.
In your second example, you're assigning to one of the pointers in the array. You could do the same with pTeamMember:
pTeams[team]->pTeamMember[i] = 0;

To set the value of a pointer to 0 (presuming you mean point at nothing), you can do the following. Note that you can use 0 or nullptr (which is valid as of C++ 11)
int *p = nullptr;
In regards to your specific example, change
pTeams[team]->pTeamMember=0;
to
pTeams[team]->pTeamMember[index]=0;

Related

C++ avoid compare NULL to 0

Since in C++, NULL is essential jus 0. Every time I have a variable uninitialized set to NULL and later on I want to check whether this variable is initialized or not by comparing it to NULL. However, if the variable happens to be assigned to 0 somewhere, then there is no way I can tell whether it has been initialized or not. There is definitely some work around I can do to avoid the situation like using another wrapped object to wrap the variable or use a flag. But I am looking for a more elegant solution. For example:
int b[4] = {1,2,3,0};
int a = NULL;
int i = 0;
while(true){
if(a == 0)return;
a = b[i++];
}
so here it will goes into the if statement right away but I want to wait
until a is assigned to 0 by b's last element
I think what you're looking for is boost::optional.
#include <boost/optional.hpp>
...
boost::optional<int> maybe_int;
...
if (maybe_int) // test if it has been assigned to
{
if (maybe_int.value() == 0) // has is been set to zero?
{
// ...
}
}
I think you're making a conceptual mistake here. Even though NULL is defined as 0 somewhere, this does not mean they are the same on a conceptual level.
NULL is a pointer with (usually) the value 0. By convention, there cannot be an object at NULL. We can therefore use code such as MyClass *obj = NULL; to specify that there is no object at obj. This works because obj is a pointer.
For non-pointer types, this does not work and we need to use other tools such as arbitrarily defining one value as invalid (often -1is used for this) or using optionals.

How to overcome null pointer initialisation error? (C/C++)

I'm having a hard time figuring out why the initialisation at NULL of one pointer is bugging my program.
In order to not receive a compilation error, almost all the pointers in my code need to be initialise as nullptr like usual. Problem is, when I tried to use them afterwards, I get a whole bunch of errors telling me the null pointer is invalid. To overcome this error, the buggy pointers are now declare as an array. This way all my problems with this initialisation are gone. Everything seems to work fine until I realise one of my conditional branch was always returning false.
The full code is a bit too long and some parts use french words, so let's just paste what interest us...
struct Numero {
int num;
char client[50];
Message *listeMessage = nullptr;
Numero *suivant = nullptr;
int nbmsg = 0;
};
char buff_char[50];
number = new Numero;
file >> number->client; // getting the right char client[50] from the file
if (number->client == buff_char)
break;
I also tried if(*number->client == *buff_char) without success. I'm now thinking that the problem is probably the array of char, but because of my previous problem I can't really change that so I'm hoping somebody can figure out what's going on here.
Note: Don't suggest anything related to strings, I can't use them because it's part of the challenge here to practice with pointers.
number->client == buff_char
Both of these are arrays, which decay into pointers. They are two different arrays with two different addresses, so this will always return false.
*number->client == *buff_char
This would compare the first element in each array, which is also not what you want.
You can use a standard library function like strcmp.
If you can't use the standard library, loop over each array until one has a different element than the other (not equal), they both have '\0' (equal), or they both reach 50 (equal).
In If do a comparison instead of assignment
if (number->client == buff_char)
In C++ = is the assignment operator. You want == for equality. What your if statement does right now is sets client equal to buff_char and then evaluates based on the newly assigned value of client.

How to dynamically allocate an array of pointers to objects?

I do not know the number of horse. I have already tried to do it, but I am getting errors and I don't know why. I am basically trying to create an array of pointers to objects. The class everything is in is called horse. Here is what I have. I am still getting errors from the c++ compiler.
horse::horse(string horse_Name, string rider_Name, int distance_Traveled, int max_Distance_Per_Second)
{
distance_Traveled = 0;
const int number_Of_Horses = 1;
horse **ptr = new horse*[number_Of_Horses];
*ptr[number_Of_Horses] =
{
horse(horse_Name, rider_Name, distance_Traveled, max_Distance_Per_Second)
};
max_Running_Distance_Per_Second(max_Distance_Per_Second);
}
distance_Traveled = 0;
Since distance_Traveled is not passed by reference, it will not change the original value. This may be one error.
horse **ptr = new horse*[number_Of_Horses];
This pointer is declared correctly; however, when you try to use iteration(or other expression, just my conjecture), you have to be careful,BECAUSE:
When it is a array, *ptr[number_Of_Horses] is illegal because an array start with 0, not 1. This is a common error. This may be a major issue.

Dynamic memory allocation initialisation in C++ class

(I'm not posting my code as this is for a project, however I have tried to get help for this issue but have had no luck)
Hi there, I am trying to initialise the size of an array of pointers (char*) which is a private member variable of my class class A
I'm using the constructor to set the size by setting an integer variable (also a member variable) which will then be used to create my array of pointers.
I have done this so far:
// Constructor - 'int value' is set to a value
private:
int value;
char ** myArray = new char*[value];
So basically I want an array of pointers in which each element can point to a string. I am passing string variables to myArray by using (char*) stringVar.c_str();
Although all of this works, I am getting some pretty weird errors when trying to store variables and have even gotten this error:
free (): invalid next size (fast)
It's weird because even when myArray is of size 4, when I try to access, say, the 3rd element, I get the same error as above.
I am very new to C++ and am very intent on solving these issues. I've had to resort to this forum for help and am looking forward to some ideas from you guys :)
if you are new C++ programmer and want work with C++ String list is better work with std::vector<std::string> for complete tutorial of how using vectors see:
http://www.cplusplus.com/reference/vector/vector/
but in you question is String list size fixed?or not?
if string list is not fixed you must malloc space for array first time in constructor and then realloc array when you want insert a string in your string list for example:
class A{
private:
char** arrayy;
int arrayysize;
A(){
arrayy = (char**)calloc(1,sizeof(char*));
arrayysize = 1;
}
insertToarrayy(char* data){
strcpy(arrayy[arrayysize-1],data);
arrayy = (char**)realloc(arrayy,arrayysize+1);
arrayysize += 1;
}
}

Putting [][] in ** [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing two-dimensional array via pointer
int table[20][20];
int** table1 = table;
int a;
table[0][0] = 4;
a = table1[0][0];
the last line gives me Access violation, and i dont get it ..
thanks in advance!
The short answer is that a pointer to a pointer is not the same as an array of arrays.
You can't make a int ** from a two dimensional int array in one assignment. First, you need to allocate the correct number of pointers:
table1 = malloc(sizeof(int*) * 20);
Then you can use a loop to fill in your next level pointers:
for(int i = 0; i < 20; i++)
table1[i] = table[i];
The reason is that pointer to pointer is ONE memory location that holds an address. When you make the first allocation, that makes that pointer point to 20 int pointers. We then assign each of those pointers the first address of each row in the original table. Now, when we use a = table1[0][0], the compiler will go fetch table1[0] - so the first pointer in the array we set up in the loop. This points to table[0] row, so we fetch the [0] element from that, and get the 4 that was stuffed in there [of course, any other number index would also get four, so it's hard to show that it works right in this case).
In the table[y][x] case, the compiler will take the address of table, add y * 20 * sizeof(int) to it, and then add x * sizeof(int). Which gives us a nice place in the "square" lump of memory that "table" is.
(Again, typing too much, two more answers since I started writing this)
C compilers are just too forgiving:
int** table1 = table;
c.c: In function ‘main’:
c.c:3:17: warning: initialization from incompatible pointer type [enabled by default]
If we feed it incorrect code, the compiler complains, and we ignore the complaint and then get some strange outcome, should we wonder?
Anyway, table1 doesn't point to a pointer to int, and the way it's going to crash is a matter of implementation (alignment, segmentation etc.)
Although table1[1] is of type pointer, there are no pointers anywhere in table.
Ask yourself: Given a pointer, and being told to access item 3, 4 - how should the compiler know how large the first dimension of your two-dimensional array is, in order to skip the right amount of items?
Correct: Not at all.
Given
int table[20][20];
table is compatible with int*, not int**.
This should compile and be able to access table1 from table1[0] to table1[399]:
int* table1 = table;