Beginner's questions

Hello...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int *piNumbers[10][10];
    for(int x = 0;x < 10;x++)
    {
        for(int y = 0;y < 10;y++)
        {
            piNumbers[x][y] = 0;
        }
    }
    for(int x = 0;x < 10;x++)
    {
        for(int y = 0;y < 10;y++)
        {
            cout << piNumbers[x][y];
        }
        cout << endl;
    }
    return 0;
}

If I put 6 instead of 0 on line 12, I get an error... Can someone explain to me why?..
thanks
What error?

It looks like a dereferencing thing, but...
invalid conversion from 'int' to 'int*'...
Yeah, that's what I thought.

Change line 7 to int piNumbers[10][10];
But I have a case here with pointers... I really don't know why I can't fill the array with anything else but 0??
Some compilers will assume that 0 means nullptr. Change the pointer, and it'll work. Otherwise you'll have to dereference piNumbers in both of your for loops.
How to dereference piNumbers?
*piNumbers[x][y];
You mean like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int *piNumbers[10][10];
    for(int x = 0;x < 10;x++)
    {
        for(int y = 0;y < 10;y++)
        {
            *piNumbers[x][y] = 6;
        }
    }
    for(int x = 0;x < 10;x++)
    {
        for(int y = 0;y < 10;y++)
        {
            cout << *piNumbers[x][y];
        }
        cout << endl;
    }
    return 0;
}


Now I get windows error... file.exe has encountered a problem and needs to close....
You haven't used new to allocate any memory for your pointer so it's pointing somewhere that you can't write to.
Is this for a dynamically allocated array?
A simple example that can help to understand using of pointers

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

using namespace std;

int main()
{
    const int N = 3;

    int a[N][N] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    int *piNumbers[N][N];

    for ( int x = 0; x < N; x++)
    {
        for( int y = 0; y < N; y++ )
        {
            piNumbers[x][y] = &a[x][y];
        }
    }

    for ( int x = 0; x < N; x++)
    {
        for( int y = 0; y < N; y++ )
        {
            cout << *piNumbers[x][y];
        }
        cout << endl;
    }

    return 0;
}
Last edited on
Hello, me again...

It's all started with an example, where someone has created a class... In that class he created an object from another class like this:

CCreature* mqpaaCreatures[10][10]

In the same class where he created this object, he has a constructor:

1
2
3
4
for(unsigned int uiRow = 0;uiRow < 10;uiRow++)  {
	for(unsigned int uiCol = 0;uiCol < 10;uiCol++)  {
		mqpaaCreatures[uiRow][uiCol] = 0;
}}


Then I try to do this with int... I can set the whole array to 0...
But when I try to put some other value, like 6, I get an error...
I understand what have you shown me above, I just need an explanation why I can set it to 0, but not to 6...
I would appreciate if you correct me if I did any grammatical errors in my posts...
Thanks...
Last edited on
From the C++ Standard : "1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Such a conversion is called a null pointer conversion."
Have another one...
Why this works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int iSize = 5;
    int *piNumbers = new int[iSize];
    for(int x = 0;x < iSize;x++)
    {
            piNumbers[x] = 6;
    }
    for(int x = 0;x < iSize;x++)
    {
            cout << piNumbers[x];
    }
    return 0;
}


and this don't:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int iSize1 = 5, iSize2 = 4;
    int *piNumbers = new int[iSize1][iSize2];
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            piNumbers[x][y] = 6;
        }
    }
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            cout << piNumbers[x][y];
        }
    }
    return 0;
}


Thanks...
A 2d array is really a pointer to a pointer so I think you need to do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int iSize1 = 5, iSize2 = 4;
    int **piNumbers = new *int[iSize1][iSize2];
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            piNumbers[x][y] = 6;
        }
    }
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            cout << piNumbers[x][y];
        }
    }
    return 0;
}
In function 'int main()'
8|error: expected type-specifier before '*' token|
8|error: expected ',' or ';' before 'int'|
||=== Build finished: 2 errors, 0 warnings ===|
Do you read what I am writting or not?! Or you fully ignore what others write to you?!
Look at your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int iSize = 5;
    int *piNumbers = new int[iSize];
    for(int x = 0;x < iSize;x++)
    {
            piNumbers[x] = 6;
    }
    for(int x = 0;x < iSize;x++)
    {
            cout << piNumbers[x];
    }
    return 0;
}


What is type of piNumbers[x]? Its type is int. What is the problem to assign integer value to an object of type int?!


vlad from moscow please ignore my posts... Your first post was what I already know(create an array an copy it's address to another location pointed by piNumbers)... The second post was the rule from the C++ Standard... The third post above, you showed me the working code... Instead of yelling, you could explain the diference between the examples with your own words...
Stewbond you were right + two lines of code...

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
#include <iostream>
 
using namespace std;
 
int main()
{
    int iSize1 = 5, iSize2 = 4;
    int **piNumbers = new int*[iSize1];
    for(int x = 0;x < iSize1;x++)
        piNumbers[x]=new int[iSize2];
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            piNumbers[x][y] = 6;
        }
    }
    for(int x = 0;x < iSize1;x++)
    {
        for(int y = 0;y < iSize2;y++)
        {
            cout << piNumbers[x][y];
        }
    }
    return 0;
}
Topic archived. No new replies allowed.