array pointer question

J

Jonathan Mcdougall

let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}

The same as an int :

int x = 5;
f(x);

// takes an int
void f(int i)
{
i = 2;
}

The reason for which 'i' is of type 'int' is because 'x' is of type
int.

Now if 'x' is of type 'int[2][2]', well 'i' will be too :

void f(int i[2][2])
{
i[0][0] = 4;
}


But if you allocated the array with new, you probably did something
like

int size_1 = 2;
int size_2 = 2;

int **x = new int*[size_1]

for ( int i = 0; i<size_1; ++i)
x = new int[size_2];


Since 'x' is now of type 'int**', that is what 'i' will be :

void f(int **i)
{
i[0][0] = 4;
}

The 'problem' with that is now you have no idea about the bounds of
'i'. You have two solutions. Either you pass the information too :

void f(int **i, int size_1, int size_2)
{
}

or you use something like the zero-terminated c-style string and
always set the last element to a given value which means end-of-array.

But know that arrays are always passed by address, that is, modifying
its value in f() will modify 'x' too.


Jonathan
 
W

WW

Dmitrii said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}

The above array looks like this:

+---+---+---+---+
|0,0|0,1|1,0|1,1|
+---+---+---+---+

Each box represent an integer. The indices are n,m as: x[n][m].
 
D

Dmitrii PapaGeorgio

let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}
 
D

Dmitrii PapaGeorgio

Dmitrii said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}

forgot...the array is being passed as func(x[0])
 
M

Mike Wahler

Dmitrii PapaGeorgio said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {

That type will only work for a single dimension array.
//i want to fill it with a number, like 1
}

#include <cstdlib>
#include <iostream>

void func(int (*x)[2])
{
x[0][0] = 1;
x[0][1] = 1;
x[1][0] = 1;
x[1][1] = 1;
}

int main()
{
int x [2][2] = {0};
func(x);

for(size_t i = 0; i < sizeof x / sizeof *x; ++i)
for(size_t j = 0; j < sizeof *x / sizeof **x; ++j)
std::cout << "x[" << i << "][" << j << "] == "
<< x[j] << '\n';

return 0;
}

Why are you using an array instead of a container?

-Mike
 
D

Dmitrii PapaGeorgio

inside the function when i try to assign a value i get an error saying
it's not a pointer or array like

x[1][2] = 1;
Dmitrii said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}


The above array looks like this:

+---+---+---+---+
|0,0|0,1|1,0|1,1|
+---+---+---+---+

Each box represent an integer. The indices are n,m as: x[n][m].
 
D

Dmitrii PapaGeorgio

i'm using array and not a container since my teacher is a fruit cake.
thanks-

Mike said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {


That type will only work for a single dimension array.

//i want to fill it with a number, like 1
}


#include <cstdlib>
#include <iostream>

void func(int (*x)[2])
{
x[0][0] = 1;
x[0][1] = 1;
x[1][0] = 1;
x[1][1] = 1;
}

int main()
{
int x [2][2] = {0};
func(x);

for(size_t i = 0; i < sizeof x / sizeof *x; ++i)
for(size_t j = 0; j < sizeof *x / sizeof **x; ++j)
std::cout << "x[" << i << "][" << j << "] == "
<< x[j] << '\n';

return 0;
}

Why are you using an array instead of a container?

-Mike
 
W

WW

Dmitrii said:
inside the function when i try to assign a value i get an error saying
it's not a pointer or array like
x[1][2] = 1;

Because inside the function you have no array, you have a pointer to the
first element of the array:

+-+ +---+---+---+---+
|x|-->|0,0|0,1|1,0|1,1|
+-+ +---+---+---+---+
0 1 2 3

Each box represent an integer. The indices inside the boxes are n,m as:
x[n][m].
 
W

WW

Dmitrii said:
i'm using array and not a container since my teacher is a fruit cake.
thanks-
[SNIP]

Fruitcakes top post. Your teacher wants you to learn arrays.
 
K

Kevin Goodsell

Jonathan said:
The same as an int :

int x = 5;
f(x);

// takes an int
void f(int i)
{
i = 2;
}

The reason for which 'i' is of type 'int' is because 'x' is of type
int.

Now if 'x' is of type 'int[2][2]', well 'i' will be too :

Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
to array of two ints). Array names usually become pointers to the type
of the objects stored in the array. There are exceptions to this, for
example when the name is used as the operand of the sizeof or unary &
operators.
void f(int i[2][2])

Equivalent to

void f(int (*i)[2])
{
i[0][0] = 4;
}


But if you allocated the array with new, you probably did something
like

int size_1 = 2;
int size_2 = 2;

int **x = new int*[size_1]

for ( int i = 0; i<size_1; ++i)
x = new int[size_2];


Since 'x' is now of type 'int**', that is what 'i' will be :

void f(int **i)
{
i[0][0] = 4;
}

The 'problem' with that is now you have no idea about the bounds of
'i'.


This isn't a new problem. You had the same problem before:

void f(int i[2][2])
{
//...
}

This doesn't give the size of the array i (which isn't actually an
array), it just looks like it does. That first '2' is absolutely useless
- the compiler ignores it.

-Kevin
 
M

Mike Wahler

Dmitrii PapaGeorgio said:
Dmitrii said:
let's say i have a 2D array, how would i access it with a pointer?

int x [2][2];

func(x);

void func(int *x) {
//i want to fill it with a number, like 1
}

forgot...the array is being passed as func(x[0])

That means that a pointer to x[0][0] is being passed.

#include <iostream>
void func(int *x)
{
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 1;
}

int main()
{
int x[2][2] = {0};
size_t i = 0;
size_t j = 0;
func(x[0]);
for(i = 0; i < sizeof x / sizeof *x; ++i)
for(j = 0; j < sizeof *x / sizeof **x; ++j)
std::cout << "x[" << i << "][" << j << "] == "
<< x[j] << '\n';
return 0;
}

-Mike


}

-Mike
 
M

Mike Wahler

Dmitrii PapaGeorgio said:
i'm using array and not a container since my teacher is a fruit cake.

It's rarely a good idea to use epithets to describe
someone who probably has much more knowledge than you do.
Are you sure your teacher doesn't read posts here?
If (s)he does, and sees this post from you, how do
you think you'll be treated in his/her class in the
future? Just a thought ....

Your post didn't mention anything about a teacher or
that you're taking a class. Your teacher is probably
trying to teach you about arrays. That doesn't make
him/her a 'fruitcake', but someone who is teaching
part of the language. Arrays do have their place
in C++ programs, but often a container is a better
choice for some tasks. That issue does not apply
here, since your array is just part of a learning
exercise and not a 'real' application.

BTW please don't top post.

-Mike
 
D

Dmitrii PapaGeorgio

thanks guys - better lectures in here than in class...hope she doesn't
read this thread (teacher)

Kevin said:
Jonathan said:
The same as an int :

int x = 5;
f(x);

// takes an int
void f(int i)
{
i = 2;
}

The reason for which 'i' is of type 'int' is because 'x' is of type
int.

Now if 'x' is of type 'int[2][2]', well 'i' will be too :


Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
to array of two ints). Array names usually become pointers to the type
of the objects stored in the array. There are exceptions to this, for
example when the name is used as the operand of the sizeof or unary &
operators.
void f(int i[2][2])


Equivalent to

void f(int (*i)[2])
{
i[0][0] = 4;
}


But if you allocated the array with new, you probably did something
like

int size_1 = 2;
int size_2 = 2;

int **x = new int*[size_1]

for ( int i = 0; i<size_1; ++i)
x = new int[size_2];


Since 'x' is now of type 'int**', that is what 'i' will be :

void f(int **i)
{
i[0][0] = 4;
}

The 'problem' with that is now you have no idea about the bounds of
'i'.



This isn't a new problem. You had the same problem before:

void f(int i[2][2])
{
//...
}

This doesn't give the size of the array i (which isn't actually an
array), it just looks like it does. That first '2' is absolutely useless
- the compiler ignores it.

-Kevin
 
J

Jonathan Mcdougall

Now if 'x' is of type 'int[2][2]', well 'i' will be too :
Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
to array of two ints). Array names usually become pointers to the type
of the objects stored in the array. There are exceptions to this, for
example when the name is used as the operand of the sizeof or unary &
operators.

Thanks for clarification.
void f(int i[2][2])

Equivalent to

void f(int (*i)[2])

Yes, but not (imho) better/clearer.

void f(int **i)
{
i[0][0] = 4;
}

The 'problem' with that is now you have no idea about the bounds of
'i'.

This isn't a new problem. You had the same problem before:

Yes, but since you wrote the dimension manually before compiling, you
know its size and can use magic numbers/constants. That is what I meant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top